Skip to content

Commit

Permalink
redefined the way data representations work (the representation funct…
Browse files Browse the repository at this point in the history
…ion is applied to the source result, rather than to the output)
  • Loading branch information
daslu committed Aug 14, 2023
1 parent c1d2023 commit 170b841
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ For example, let us add support for [tech.ml.dataset](https://github.com/techasc
"tech.ml.dataset dataset"
{:predicate tc/dataset?
:representation (fn [ds]
`(tc/dataset ~(-> ds
(update-vals vec)
(->> (into {})))))})
(-> ds
(update-vals vec)
(->> (into {}))))})
```

Now, a code example like
Expand All @@ -88,29 +88,28 @@ Now, a code example like
```
will result in a test like
```clj
(deftest test-3
(is (=
(deftest test-4
(is (->
(-> {:x [1 2 3]}
tc/dataset
(tc/map-columns :y [:x] (partial * 10)))
;; =>
(tablecloth.api/dataset {:x [1 2 3], :y [10 20 30]}))))
note-to-test/represent-value
(=
{:x [1 2 3], :y [10 20 30]}))))
```

You see, the output value is represented as a code snippet that would generate that value. Since `tech.ml.dataset` datasets can be compared using the `=` function, this is a valid test for our code example.

## Wishlist
- support running from command line
- support an alternative plain-data output format to help seeing the output changes explicitly
- make clear error messages:
- when outputs change
- when the code fails
- when outputs change
- when the code fails
- support code examples in comment blocks?
- support metadata to skip certain forms
- remove nil outputs?
- support docstrings
- generate tests from code examples in docstrings
- explore generate docstrings in a structured way (marking code examples explicitly)
- generate tests from code examples in docstrings
- explore generate docstrings in a structured way (marking code examples explicitly)
- support approximate comparisons of values for floating-point computations
- support automatic regeneration (say, using file watch)

Expand Down
6 changes: 3 additions & 3 deletions notebooks/dum/dummy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"tablecloth dataset"
{:predicate tc/dataset?
:representation (fn [ds]
`(tc/dataset ~(-> ds
(update-vals vec)
(->> (into {})))))})
(-> ds
(update-vals vec)
(->> (into {}))))})

(+ 4
5
Expand Down
6 changes: 6 additions & 0 deletions src/scicloj/note_to_test/v1/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@
"
[name spec]
(impl/define-value-representation! name spec))


(defn represent-value
"Represent a given value `v` using the extensible definitions of special value representations."
[v]
(impl/represent-value v))
9 changes: 5 additions & 4 deletions src/scicloj/note_to_test/v1/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
(def test-template
"
(deftest %s
(is (=
(is (->
%s
;; =>
%s)))
note-to-test/represent-value
(=
%s))))
")

(defn ->test [code index source-ns]
Expand All @@ -71,7 +72,7 @@
represent-value
pp/pprint
with-out-str
(indent 4))))))
(indent 5))))))



Expand Down
41 changes: 23 additions & 18 deletions test/dum/dummy_generated_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,52 @@
[tablecloth.api :as tc]))

(deftest test-0
(is (=
(is (->
(note-to-test/define-value-representation!
"tablecloth dataset"
{:predicate tc/dataset?
:representation (fn [ds]
`(tc/dataset ~(-> ds
(update-vals vec)
(->> (into {})))))})
;; =>
:ok)))
(-> ds
(update-vals vec)
(->> (into {}))))})
note-to-test/represent-value
(=
:ok))))


(deftest test-1
(is (=
(is (->
(+ 4
5
6)
;; =>
15)))
note-to-test/represent-value
(=
15))))

(defn f [x]
(+ x 19))

(deftest test-3
(is (=
(is (->
(f 12)
;; =>
31)))
note-to-test/represent-value
(=
31))))


(deftest test-4
(is (=
(is (->
(-> {:x [1 2 3]}
tc/dataset
(tc/map-columns :y [:x] (partial * 10)))
;; =>
(tablecloth.api/dataset {:x [1 2 3], :y [10 20 30]}))))
note-to-test/represent-value
(=
{:x [1 2 3], :y [10 20 30]}))))


(deftest test-5
(is (=
(is (->
(f 13)
;; =>
32)))
note-to-test/represent-value
(=
32))))

0 comments on commit 170b841

Please sign in to comment.