Skip to content

Commit 75abd0f

Browse files
committed
Adding more tests
1 parent c806cde commit 75abd0f

File tree

7 files changed

+86
-31
lines changed

7 files changed

+86
-31
lines changed

priv/build/iife/ElixirScript.Core.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/javascript/lib/core/erlang_compat/unicode.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
import erlang from './erlang';
2+
import lists from './lists';
23

3-
function characters_to_list(characters) {
4-
return characters.split('').map(c => c.codePointAt(0));
4+
function characters_to_list(characters, inEncoding = Symbol.for('unicode')) {
5+
let values = characters;
6+
7+
if (Array.isArray(characters)) {
8+
values = lists.flatten(characters);
9+
}
10+
11+
if (erlang.is_binary(values)) {
12+
return values.split('').map(c => c.codePointAt(0));
13+
}
14+
15+
return values.reduce((acc, c) => {
16+
if (erlang.is_integer(c)) {
17+
return acc.concat(c);
18+
}
19+
20+
return acc.concat(characters_to_list(c, inEncoding));
21+
}, []);
522
}
623

724
function characters_to_binary(characters) {
8-
if (erlang.is_binary(characters)) {
9-
return characters;
10-
}
25+
const values = characters_to_list(characters);
1126

12-
return String.fromCodePoint(...characters);
27+
return String.fromCodePoint(...values);
1328
}
1429

1530
export default {

src/javascript/lib/core/functions.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
import Protocol from './protocol';
22
import Core from '../core';
33

4-
function iterator_to_reducer(iterable, acc, fun) {
5-
const iterator = iterable[Symbol.iterator]();
6-
let x = iterator.next();
7-
let _acc = acc;
8-
9-
while (x.done === false) {
10-
_acc = fun(x.value, _acc.get(1));
11-
if (_acc.get(0) === Symbol.for('halt')) {
12-
return new Core.Tuple(Symbol.for('halted'), _acc.get(1));
13-
} else if (_acc.get(0) === Symbol.for('suspend')) {
14-
return new Core.Tuple(Symbol.for('suspended'), _acc.get(1), new_acc => {
15-
return iterator_to_reducer(iterator, new_acc, fun);
16-
});
17-
}
18-
19-
x = iterator.next();
20-
}
21-
22-
return new Core.Tuple(Symbol.for('done'), _acc.get(1));
23-
}
24-
254
function call_property(item, property) {
265
if (!property) {
276
if (item instanceof Function || typeof item === 'function') {
@@ -148,7 +127,6 @@ export default {
148127
defprotocol,
149128
defimpl,
150129
build_namespace,
151-
iterator_to_reducer,
152130
map_to_object,
153131
trampoline,
154132
Recurse
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('copy', t => {
5+
let result = Core.binary.copy('h', 3);
6+
t.deepEqual(result, 'hhh');
7+
8+
result = Core.binary.copy('h');
9+
t.deepEqual(result, 'h');
10+
});
11+
12+
test('list_to_bin', t => {
13+
let result = Core.binary.list_to_bin([104, 101, 108, 108, 111]);
14+
t.deepEqual(result, 'hello');
15+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('warn', t => {
5+
let result = Core.elixir_errors.warn(['this is a warning']);
6+
t.deepEqual(result, Symbol.for('ok'));
7+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('put_chars', t => {
5+
let result = Core.io.put_chars(Symbol.for('stdio'), 'Hello');
6+
t.deepEqual(result, Symbol.for('ok'));
7+
8+
result = Core.io.put_chars(Symbol.for('stderr'), 'Hello');
9+
t.deepEqual(result, Symbol.for('ok'));
10+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('characters_to_list', t => {
5+
let result = Core.unicode.characters_to_list('hello');
6+
t.deepEqual(result, [104, 101, 108, 108, 111]);
7+
8+
result = Core.unicode.characters_to_list(['hello', 'fg']);
9+
t.deepEqual(result, [104, 101, 108, 108, 111, 102, 103]);
10+
11+
result = Core.unicode.characters_to_list(['hello', 'fg', 34]);
12+
t.deepEqual(result, [104, 101, 108, 108, 111, 102, 103, 34]);
13+
14+
result = Core.unicode.characters_to_list(['hello', 'fg', 34, ['s']]);
15+
t.deepEqual(result, [104, 101, 108, 108, 111, 102, 103, 34, 115]);
16+
});
17+
18+
test('characters_to_binary', t => {
19+
let result = Core.unicode.characters_to_binary('hello');
20+
t.deepEqual(result, 'hello');
21+
22+
result = Core.unicode.characters_to_binary(['hello', 'fg']);
23+
t.deepEqual(result, 'hellofg');
24+
25+
result = Core.unicode.characters_to_binary(['hello', 'fg', 34]);
26+
t.deepEqual(result, 'hellofg"');
27+
28+
result = Core.unicode.characters_to_binary(['hello', 'fg', 34, ['s']]);
29+
t.deepEqual(result, 'hellofg"s');
30+
});

0 commit comments

Comments
 (0)