Skip to content

Commit

Permalink
Create testing code for the Nock and Noun module
Browse files Browse the repository at this point in the history
We test:
1. Nock
2. Noun
3. Noun.Format

further we write some custom hoon functions that compile to nock using
our standard library, making sure that nock evaluation works as
expected.

The Noun tests mainly test inserting and indexing
  • Loading branch information
mariari committed Dec 19, 2023
1 parent 59ec820 commit 64f1f9e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
16 changes: 16 additions & 0 deletions hoon/tests.hoon
Original file line number Diff line number Diff line change
@@ -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)
==
5 changes: 0 additions & 5 deletions lib/noun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions test/nock_test.exs
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions test/noun_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64f1f9e

Please sign in to comment.