aboutsummaryrefslogtreecommitdiff
path: root/js/tests/reader.js
blob: 132ec4fa2286d95a30c1099b6b4a983e78ed89e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
common = require('./common.js');
types = require('../types');
reader = require('../reader');
core = require('../core');
var assert_eq = common.assert_eq,
    read_str = reader.read_str,
    nth = core.ns.nth;

console.log("Testing read of constants/strings");
assert_eq(2,read_str('2'));
assert_eq(12345,read_str('12345'));
assert_eq(12345,read_str('12345 "abc"'));
assert_eq('abc',read_str('"abc"'));
assert_eq('a string (with parens)',read_str('"a string (with parens)"'));

console.log("Testing read of symbols");
assert(types._symbol_Q(read_str('abc')));
assert_eq('abc',read_str('abc').value);
assert_eq('.',read_str('.').value);

console.log("Testing READ_STR of strings");
assert_eq('a string',read_str('"a string"'));
assert_eq('a string (with parens)',read_str('"a string (with parens)"'));
assert_eq('a string',read_str('"a string"()'));
assert_eq('a string',read_str('"a string"123'));
assert_eq('a string',read_str('"a string"abc'));
assert_eq('',read_str('""'));
assert_eq('abc ',read_str('"abc "'));
assert_eq(' abc',read_str('" abc"'));
assert_eq('$abc',read_str('"$abc"'));
assert_eq('abc$()',read_str('"abc$()"'));
assert_eq('"xyz"',read_str('"\\"xyz\\""'));


console.log("Testing READ_STR of lists");
assert_eq(2,core.ns.count(read_str('(2 3)')));
assert_eq(2,core.ns.first(read_str('(2 3)')));
assert_eq(3,core.ns.first(core.ns.rest(read_str('(2 3)'))));
L = read_str('(+ 1 2 "str1" "string (with parens) and \'single quotes\'")');
assert_eq(5,core.ns.count(L));
assert_eq('str1',nth(L,3));
assert_eq('string (with parens) and \'single quotes\'',nth(L,4));
assert_eq([2,3],read_str('(2 3)'));
assert_eq([2,3, 'string (with parens)'],read_str('(2 3 "string (with parens)")'));


console.log("Testing READ_STR of quote/quasiquote");
assert_eq('quote',nth(read_str('\'1'),0).value);
assert_eq(1,nth(read_str('\'1'),1));
assert_eq('quote',nth(read_str('\'(1 2 3)'),0).value);
assert_eq(3,nth(nth(read_str('\'(1 2 3)'),1),2));

assert_eq('quasiquote',nth(read_str('`1'),0).value);
assert_eq(1,nth(read_str('`1'),1));
assert_eq('quasiquote',nth(read_str('`(1 2 3)'),0).value);
assert_eq(3,nth(nth(read_str('`(1 2 3)'),1),2));

assert_eq('unquote',nth(read_str('~1'),0).value);
assert_eq(1,nth(read_str('~1'),1));
assert_eq('unquote',nth(read_str('~(1 2 3)'),0).value);
assert_eq(3,nth(nth(read_str('~(1 2 3)'),1),2));

assert_eq('splice-unquote',nth(read_str('~@1'),0).value);
assert_eq(1,nth(read_str('~@1'),1));
assert_eq('splice-unquote',nth(read_str('~@(1 2 3)'),0).value);
assert_eq(3,nth(nth(read_str('~@(1 2 3)'),1),2));


console.log("All tests completed");