diff --git a/hoon/tests.hoon b/hoon/tests.hoon new file mode 100644 index 000000000..64b4a9ed4 --- /dev/null +++ b/hoon/tests.hoon @@ -0,0 +1,16 @@ +:: Load anoma.hoon into your environemnt as anoma +:: then write .* name [0 2] to get the arm +=use-dec => anoma +|= a=@ud +(dec a) + +=fib => anoma +|= n=@ud +=+ [b=1 a=0] +|- +?: =(n 0) a +%= $ + a b + b (add a b) + n (dec n) +== diff --git a/lib/noun.ex b/lib/noun.ex index 4c457bba1..5a0de827b 100644 --- a/lib/noun.ex +++ b/lib/noun.ex @@ -17,11 +17,6 @@ defmodule Noun do defguard is_even(term) when is_noun_atom(term) and Integer.is_even(term) defguard is_odd(term) when is_noun_atom(term) and Integer.is_odd(term) - @testing_noun Noun.Format.parse_always("[[4 5] [12 13] 7]") - def testing_noun do - @testing_noun - end - @spec axis(non_neg_integer(), t()) :: {:ok, t()} | :error def axis(axis, noun) do try do diff --git a/test/nock_test.exs b/test/nock_test.exs new file mode 100644 index 000000000..03e2fe266 --- /dev/null +++ b/test/nock_test.exs @@ -0,0 +1,57 @@ +defmodule AnomaTest.Nock do + use ExUnit.Case, async: true + + import Nock + import Noun + + doctest(Nock) + + def using_dec_core() do + arm = Noun.Format.parse_always("[8 [9 342 0 7] 9 2 10 [6 0 14] 0 2]") + sample = 999 + [arm, sample | stdlib_core()] + end + + def factorial() do + arm = Noun.Format.parse_always(" + [ 8 + [1 1 0] + 8 + [ 1 + 6 + [5 [0 30] 1 0] + [0 13] + 9 + 2 + 10 + [30 8 [9 342 0 31] 9 2 10 [6 0 62] 0 2] + 10 + [6 [8 [9 20 0 31] 9 2 10 [6 [0 29] 0 28] 0 2] 0 12] + 0 + 1 + ] + 9 + 2 + 0 + 1 + ]") + sample = 1 + [arm, sample | stdlib_core()] + end + + describe "Basic functionality" do + test "base call" do + assert nock(using_dec_core(), [9, 2, 0 | 1]) == {:ok, 998} + end + + test "call with changing arguments" do + assert nock(using_dec_core(), [9, 2, 10, [6, 1 | 5], 0 | 1]) == {:ok, 4} + end + end + + describe "Standard Library" do + test "calling fib" do + assert nock(factorial(), [9, 2, 10, [6, 1 | 7], 0 | 1]) == {:ok, 13} + end + end +end diff --git a/test/noun_test.exs b/test/noun_test.exs new file mode 100644 index 000000000..e11a8d5f5 --- /dev/null +++ b/test/noun_test.exs @@ -0,0 +1,41 @@ +defmodule AnomaTest.Noun do + use ExUnit.Case, async: true + + import Noun + alias Noun.Format + + doctest(Noun) + doctest(Noun.Format) + + @testing_noun Noun.Format.parse_always("[[4 5] [12 13] 7]") + def testing_noun() do + @testing_noun + end + + test "parse sensible terms" do + assert {:ok, [1 | 2]} = Noun.Format.parse("[1 2]") + assert {:ok, [1, [3 | 5] | 2]} = Noun.Format.parse("[1 [[3 5] 2]]") + end + + test "don't parse incorrect terms" do + assert :error = Noun.Format.parse("[[[[1]]]]") + assert :error = Noun.Format.parse("[[[[") + assert :error = Noun.Format.parse("]]]]") + end + + test "indexing" do + assert axis(1, testing_noun()) == {:ok, testing_noun()} + assert axis(2, testing_noun()) == {:ok, [4 | 5]} + assert axis(6, testing_noun()) == {:ok, [12 | 13]} + assert axis(7, testing_noun()) == {:ok, 7} + assert axis(4, testing_noun()) == {:ok, 4} + assert axis(5, testing_noun()) == {:ok, 5} + assert axis(12, testing_noun()) == {:ok, 12} + assert axis(13, testing_noun()) == {:ok, 13} + end + + test "inserting" do + assert {:ok, term} = replace(2, 2, testing_noun()) + assert axis(2, term) == {:ok, 2} + end +end