aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/otto/underscore_test.go
blob: 96c8f2e93c5094b320fdcd7a3a21cb0cd6f25864 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package otto

import (
    "./terst"
    "testing"

    "github.com/robertkrimen/otto/underscore"
)

func init() {
    underscore.Disable()
}

// A persistent handle for the underscore tester
// We do not run underscore tests in parallel, so it is okay to stash globally
// (Maybe use sync.Pool in the future...)
var tester_ *_tester

// A tester for underscore: test_ => test(underscore) :)
func test_(arguments ...interface{}) (func(string, ...interface{}) Value, *_tester) {
    tester := tester_
    if tester == nil {
        tester = newTester()
        tester.underscore() // Load underscore and testing shim, etc.
        tester_ = tester
    }

    return tester.test, tester
}

func (self *_tester) underscore() {
    vm := self.vm
    _, err := vm.Run(underscore.Source())
    if err != nil {
        panic(err)
    }

    vm.Set("assert", func(call FunctionCall) Value {
        if !toBoolean(call.Argument(0)) {
            message := "Assertion failed"
            if len(call.ArgumentList) > 1 {
                message = toString(call.ArgumentList[1])
            }
            t := terst.Caller().T()
            is(message, nil)
            t.Fail()
            return FalseValue()
        }
        return TrueValue()
    })

    vm.Run(`
        var templateSettings;

        function _setup() {
            templateSettings = _.clone(_.templateSettings);
        }

        function _teardown() {
            _.templateSettings = templateSettings;
        }

        function module() {
            /* Nothing happens. */
        }
    
        function equals(a, b, emit) {
            assert(a == b, emit + ", <" + a + "> != <" + b + ">");
        }
        var equal = equals;

        function notStrictEqual(a, b, emit) {
            assert(a !== b, emit);
        }

        function strictEqual(a, b, emit) {
            assert(a === b, emit);
        }

        function ok(a, emit) {
            assert(a, emit);
        }

        function raises(fn, want, emit) {
            var have, _ok = false;
            if (typeof want === "string") {
                emit = want;
                want = null;
            }
            
            try {
                fn();
            } catch(tmp) {
                have = tmp;
            }
            
            if (have) {
                if (!want) {
                    _ok = true;
                }
                else if (want instanceof RegExp) {
                    _ok = want.test(have);
                }
                else if (have instanceof want) {
                    _ok = true
                }
                else if (want.call({}, have) === true) {
                    _ok = true;
                }
            }
            
            ok(_ok, emit);
        }

        function test(name){
            _setup()
            try {
                templateSettings = _.clone(_.templateSettings);
                if (arguments.length == 3) {
                    count = 0
                    for (count = 0; count < arguments[1]; count++) {
                        arguments[2]()
                    }
                } else {
                    // For now.
                    arguments[1]()
                }
            }
            finally {
                _teardown()
            }
        }

        function deepEqual(a, b, emit) {
            // Also, for now.
            assert(_.isEqual(a, b), emit)
        }
    `)
}

func Test_underscore(t *testing.T) {
    tt(t, func() {
        test, _ := test_()

        test(`
            _.map([1, 2, 3], function(value){
                return value + 1
            })
        `, "2,3,4")

        test(`
            abc = _.find([1, 2, 3, -1], function(value) { return value == -1 })
        `, -1)

        test(`_.isEqual(1, 1)`, true)
        test(`_.isEqual([], [])`, true)
        test(`_.isEqual(['b', 'd'], ['b', 'd'])`, true)
        test(`_.isEqual(['b', 'd', 'c'], ['b', 'd', 'e'])`, false)
        test(`_.isFunction(function(){})`, true)
        test(`_.template('<p>\u2028<%= "\\u2028\\u2029" %>\u2029</p>')()`, "<p>\u2028\u2028\u2029\u2029</p>")
    })
}

// TODO Test: typeof An argument reference
// TODO Test: abc = {}; abc == Object(abc)