Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/tests/serverjs/file/iterator.js
blob: 603bb51d76f23c469f3677c6fb05243a57f8dd68 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

var assert = require("test/assert");
var fs = require("file");

/* a decorator that passes a path object corresponding
   to the test name and removes any files created
   therein afterward */
var Test = function (block) {
    var args = arguments;
    var exported = function () {
        for (var name in exports) {
            if (exports[name] === exported) {
                try {
                    var path = fs.path(
                        fs.resolve(module.path, '.'),
                        name
                    );
                    block(path);
                } finally {
                    if (path.exists())
                        path.rmtree();
                }
            }
        }
    };
    return exported;
};

exports.testPrintReadLine = Test(function (path) {
    var stream = path.open('w');
    stream.print('hello');
    stream.print('world');
    stream.close();
    stream = path.open('r');
    assert.is('hello\n', stream.readLine());
    assert.is('world\n', stream.readLine());
    assert.is('', stream.readLine());
});

exports.testPrintReadLineChain = Test(function (path) {
    var stream = path.open('w');
    stream.print('hello').print('world');
    stream.close();
    stream = path.open('r');
    assert.is('hello\n', stream.readLine());
    assert.is('world\n', stream.readLine());
    assert.is('', stream.readLine());
});

exports.testReadLines = Test(function (path) {
    var stream = path.open('w');
    stream.print('hello').print('world');
    stream.close();
    stream = path.open('r');
    assert.eq(['hello\n', 'world\n'], stream.readLines());
});

exports.testForEach = Test(function (path) {
    var output = path.open('w');
    var input = path.open('r');
    output.print('1');
    output.print('1');
    var count = 0;
    input.forEach(function (line) {
        assert.eq('1', line);
        count++;
    });
    assert.eq(2, count);
    output.print('2').print('2');
    input.forEach(function (line) {
        assert.eq('2', line);
        count++;
    });
    assert.eq(4, count);
    output.close();
    input.close();
});

exports.testNext = Test(function (path) {
    path.open('w').print('1').print('2').close();
    var iterator = path.open();
    assert.is('1', iterator.next());
    assert.is('2', iterator.next());
    assert.throwsError(function () {
        iterator.next();
    });
});

exports.testIterator = Test(function (path) {
    path.open('w').print('1').print('2').close();
    var iterator = path.open().iterator();
    assert.is('1', iterator.next());
    assert.is('2', iterator.next());
    assert.throwsError(function () {
        iterator.next();
    });
});

if (require.main === module.id)
    require("os").exit(require("test/runner").run(exports));