From 540bfc3e47ec39fa4bb492279ac546196106c4c5 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 24 Oct 2023 22:03:52 -0700 Subject: [PATCH 1/2] fixes and tests annotating kinds --- .github/workflows/ci.yml | 47 +++++++++++++++++ deps.edn | 23 ++++----- src/scicloj/kindly_advice/v1/completion.cljc | 50 +++++++++++-------- .../kindly_advice/v1/completion_test.cljc | 21 ++++++++ 4 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 test/scicloj/kindly_advice/v1/completion_test.cljc diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..111c275 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,47 @@ +name: kindly-advice CI + +on: [push] + +jobs: + + clojure: + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Prepare Java + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: '8' + + - name: Install Clojure tools + uses: DeLaGuardo/setup-clojure@11.0 + with: + cli: latest # Clojure CLI based on tools.deps + + # Optional step: + - name: Cache Clojure dependencies + uses: actions/cache@v3 + with: + path: | + ~/.m2/repository + ~/.gitlibs + ~/.deps.clj + # List all files containing dependencies: + key: cljdeps-${{ hashFiles('deps.edn') }} + restore-keys: cljdeps- + + - name: Execute tests + run: clojure -M:test -m cognitect.test-runner + +# TODO: for this to work: change myusername to a valid Clojars username, generate a token, add it as a secret in github project settings +# - name: Deploy +# if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'org.scicloj/kindly-advice' +# env: +# CLOJARS_PASSWORD: ${{ secrets.CLOJARSTOKEN }} +# CLOJARS_USERNAME: myusername +# run: clojure -T:build jar && clojure -T:build deploy diff --git a/deps.edn b/deps.edn index a6a85e2..93321e7 100644 --- a/deps.edn +++ b/deps.edn @@ -1,13 +1,12 @@ -{:deps {org.clojure/clojure {:mvn/version "1.10.3"} - org.scicloj/kindly {:mvn/version "4-alpha3"}} - :aliases {:dev {:extra-deps {org.scicloj/clay {:mvn/version "2-alpha32"} - scicloj/tablecloth {:mvn/version "7.000-beta-51"}} - :extra-paths ["notebooks"]} - :build {:deps {io.github.seancorfield/build-clj - {:git/tag "v0.6.4" :git/sha "c21cfde"}} +{:deps {org.clojure/clojure {:mvn/version "1.11.1"}} + :aliases {:dev {:extra-deps {org.scicloj/clay {:mvn/version "2-alpha32"} + scicloj/tablecloth {:mvn/version "7.000-beta-51"}} + :extra-paths ["test" "notebooks"]} + ;; Run tests with `clojure -T:build` + :build {:deps {io.github.seancorfield/build-clj + {:git/tag "v0.6.4" :git/sha "c21cfde"}} :ns-default build} - :test {:extra-paths ["test"] - :extra-deps {org.clojure/test.check {:mvn/version "1.1.1"} - io.github.cognitect-labs/test-runner - {:git/tag "v0.5.0" :git/sha "48c3c67"} - org.scicloj/clay {:mvn/version "2-alpha32"}}}}} + ;; Run tests with `clojure -M:test -m cognitect.test-runner` + :test {:extra-paths ["test"] + :extra-deps {org.scicloj/kindly {:mvn/version "4-alpha7"} + io.github.cognitect-labs/test-runner {:git/tag "v0.5.0" :git/sha "48c3c67"}}}}} diff --git a/src/scicloj/kindly_advice/v1/completion.cljc b/src/scicloj/kindly_advice/v1/completion.cljc index f4f0a1f..d416462 100644 --- a/src/scicloj/kindly_advice/v1/completion.cljc +++ b/src/scicloj/kindly_advice/v1/completion.cljc @@ -1,4 +1,5 @@ -(ns scicloj.kindly-advice.v1.completion) +(ns scicloj.kindly-advice.v1.completion + (:require [clojure.string :as str])) (defn eval-in-ns [ns form] (if ns @@ -16,31 +17,36 @@ (throw (ex-info "context missing both form and value" {:context context}))))) -(defn form-meta-kind [form] - (when-let [m (some-> form meta)] - (or (some->> m - :tag - resolve - deref - namespace - (= "kind")) - (some->> m - keys - (filter #(-> % - namespace - (= "kind"))) - first)))) - -(defn value-meta-kind [value] - (-> value - meta - :kindly/kind)) +(defn kind [x] + (cond (keyword? x) (when (= (namespace x) "kind") + x) + (symbol? x) (when (= (namespace x) "kind") + (keyword x)) + (fn? x) (let [tag-str (str x)] + (some-> (re-find #".*\.(kind\$.*)@.*" tag-str) + (second) + (str/replace \$ \/) + (keyword))))) + +(defn meta-kind [x] + (when-let [m (meta x)] + (or + ;; ^{:kindly/kind :kind/table} x + (kind (:kindly/kind m)) + + ;; ^kind/table x + (kind (:tag m)) + + ;; ^:kind/table x + (->> (keys m) + (keep kind) + (first))))) (defn complete-meta-kind [{:keys [form value] :as context}] (assoc context - :meta-kind (or (form-meta-kind form) - (value-meta-kind value)))) + :meta-kind (or (meta-kind form) + (meta-kind value)))) (defn complete [context] (-> context diff --git a/test/scicloj/kindly_advice/v1/completion_test.cljc b/test/scicloj/kindly_advice/v1/completion_test.cljc new file mode 100644 index 0000000..ce9e319 --- /dev/null +++ b/test/scicloj/kindly_advice/v1/completion_test.cljc @@ -0,0 +1,21 @@ +(ns scicloj.kindly-advice.v1.completion-test + (:require [clojure.test :refer [deftest is testing]] + [scicloj.kindly.v4.kind :as kind] + [scicloj.kindly-advice.v1.completion :as kac])) + +(deftest meta-kind-test + (testing "valid ways to annotate a kind" + (is (= :kind/table (kac/meta-kind (kind/table {})))) + (is (= :kind/table (kac/meta-kind ^{:kindly/kind :kind/table} {}))) + (is (= :kind/table (kac/meta-kind ^{:kindly/kind kind/table} {}))) + (is (= :kind/table (kac/meta-kind ^{:kindly/kind 'kind/table} {}))) + (is (= :kind/table (kac/meta-kind ^{kind/table true} {}))) + (is (= :kind/table (kac/meta-kind ^{'kind/table true} {}))) + (is (= :kind/table (kac/meta-kind ^:kind/table {}))) + (is (= :kind/table (kac/meta-kind ^kind/table {})))) + (testing "invalid kinds" + (is (= nil (kac/kind :kindly/table))) + (is (= nil (kac/meta-kind ^:kindly/table {}))) + (is (= nil (kac/kind 1))) + (is (= nil (kac/meta-kind ^{:kindly/kind 1} {}))) + (is (= nil (kac/meta-kind ^{:kindly/kind :not-a-kind} {}))))) From 32221a4957dcf55d00c577ffc3aea4b9217ce4ba Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 24 Oct 2023 22:48:07 -0700 Subject: [PATCH 2/2] check for meta-data on var values --- src/scicloj/kindly_advice/v1/completion.cljc | 26 ++++++++++--------- .../kindly_advice/v1/completion_test.cljc | 14 +++++++++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/scicloj/kindly_advice/v1/completion.cljc b/src/scicloj/kindly_advice/v1/completion.cljc index d416462..2d58e6c 100644 --- a/src/scicloj/kindly_advice/v1/completion.cljc +++ b/src/scicloj/kindly_advice/v1/completion.cljc @@ -29,18 +29,20 @@ (keyword))))) (defn meta-kind [x] - (when-let [m (meta x)] - (or - ;; ^{:kindly/kind :kind/table} x - (kind (:kindly/kind m)) - - ;; ^kind/table x - (kind (:tag m)) - - ;; ^:kind/table x - (->> (keys m) - (keep kind) - (first))))) + (or + (when-let [m (meta x)] + (or + ;; ^{:kindly/kind :kind/table} x + (kind (:kindly/kind m)) + + ;; ^kind/table x + (kind (:tag m)) + + ;; ^:kind/table x + (->> (keys m) + (keep kind) + (first)))) + (when (var? x) (meta-kind @x)))) (defn complete-meta-kind [{:keys [form value] :as context}] diff --git a/test/scicloj/kindly_advice/v1/completion_test.cljc b/test/scicloj/kindly_advice/v1/completion_test.cljc index ce9e319..f16091e 100644 --- a/test/scicloj/kindly_advice/v1/completion_test.cljc +++ b/test/scicloj/kindly_advice/v1/completion_test.cljc @@ -3,6 +3,13 @@ [scicloj.kindly.v4.kind :as kind] [scicloj.kindly-advice.v1.completion :as kac])) +(def table3 (kind/table {})) + +(def ^{:kindly/kind :kind/table} table4 {}) + +(def ^:kind/table table5 {}) + + (deftest meta-kind-test (testing "valid ways to annotate a kind" (is (= :kind/table (kac/meta-kind (kind/table {})))) @@ -18,4 +25,9 @@ (is (= nil (kac/meta-kind ^:kindly/table {}))) (is (= nil (kac/kind 1))) (is (= nil (kac/meta-kind ^{:kindly/kind 1} {}))) - (is (= nil (kac/meta-kind ^{:kindly/kind :not-a-kind} {}))))) + (is (= nil (kac/meta-kind ^{:kindly/kind :not-a-kind} {})))) + (testing "tricky kinds" + (is (= :kind/table (kac/meta-kind table3))) + (is (= :kind/table (kac/meta-kind #'table3))) + (is (= :kind/table (kac/meta-kind #'table4))) + (is (= :kind/table (kac/meta-kind #'table5)))))