aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure.Tests/LibTests/KeywordTests.cs
blob: 6960f14c9a086360e34326c0e4a1b2160125aefe (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 *   Copyright (c) David Miller. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using NUnit.Framework;
using Rhino.Mocks;

using clojure.lang;

using RMExpect = Rhino.Mocks.Expect;
using System.Collections;


namespace Clojure.Tests.LibTests
{
    [TestFixture]
    public class KeywordTests : AssertionHelper
    {

        #region c-tor tests

        [Test]
        public void InternCreatesKeywordBasedOnSymbol()
        {
            Symbol sym = Symbol.intern("def","abc");
            Keyword k1 = Keyword.intern(sym);
            Expect(k1.Name,EqualTo(sym.Name));
            Expect(k1.Namespace,EqualTo(sym.Namespace));
        }

        [Test]
        public void InternReturnsSameKeywordOnEqualSym()
        {
            Symbol sym1 = Symbol.intern("def", "abc");
            Symbol sym2 = Symbol.intern("def", "abc");
            Keyword k1 = Keyword.intern(sym1);
            Keyword k2 = Keyword.intern(sym2);

            Expect(Object.ReferenceEquals(k1, k2));
        }

        [Test]
        public void Intern2CreatesKeywordBasedOnSymbol()
        {
            Keyword k1 = Keyword.intern("def","abc");
            Expect(k1.Name, EqualTo("abc"));
            Expect(k1.Namespace, EqualTo("def"));
        }

        [Test]
        public void Intern2ReturnsSameKeywordOnEqualSym()
        {
            Keyword k1 = Keyword.intern("def", "abc");
            Keyword k2 = Keyword.intern("def", "abc");

            Expect(Object.ReferenceEquals(k1, k2));
        }

        #endregion

        #region object override tests

        [Test]
        public void ToStringReturnsStringNameWithColonPrepended()
        {
            Symbol sym1 = Symbol.intern("abc");
            Symbol sym2 = Symbol.intern("abc/def");
            Keyword k1 = Keyword.intern(sym1);
            Keyword k2 = Keyword.intern(sym2);

            Expect(k1.ToString(), EqualTo(":abc"));
            Expect(k2.ToString(), EqualTo(":abc/def"));
        }

        [Test]
        public void EqualOnIdentityIsTrue()
        {
            Symbol sym1 = Symbol.intern("abc");
            Keyword k1 = Keyword.intern(sym1);

            Expect(k1.Equals(k1));
        }

        [Test]
        public void EqualsOnNonKeywordIsFalse()
        {
            Symbol sym1 = Symbol.intern("abc");
            Keyword k1 = Keyword.intern(sym1);

            Expect(k1.Equals(sym1), False);
        }

        //[Test]
        //public void EqualsDependsOnSym()
        //{
        //    Symbol sym1 = Symbol.intern("abc");
        //    Symbol sym2 = Symbol.intern("abc");
        //    Keyword k1 = Keyword.intern(sym1);
        //    Keyword k2 = Keyword.intern(sym2);
        //    // I don't know how we ever create two keywords that will force
        //    // the code to go into the sym.equals part of the code.
        //    // At least, not through the factory methods.
        //}

        [Test]
        public void HashCodeDependsOnValue()
        {
            Symbol sym1 = Symbol.intern("abc");
            Symbol sym2 = Symbol.intern("abc/def");
            Keyword k1 = Keyword.intern(sym1);
            Keyword k2 = Keyword.intern(sym2);

            Expect(k1.GetHashCode(), Not.EqualTo(k2.GetHashCode()));
        }


        #endregion

        #region Named tests

        public void NameAndNamespaceComeFromTheSymbol()
        {
            Symbol sym1 = Symbol.intern("def", "abc");
            Keyword k1 = Keyword.intern(sym1);
            Symbol sym2 = Symbol.intern("abc");
            Keyword k2 = Keyword.intern(sym2);
            Expect(k1.Name, EqualTo("abc"));
            Expect(k1.Namespace, EqualTo("def"));
            Expect(k2.Name, EqualTo("abc"));
            Expect(k2.Namespace, Null);
        }

        #endregion
        
        #region IFn Tests

        [Test]
        public void Invoke2IndexesIntoItsFirstArg()
        {
            Keyword k1 = Keyword.intern(Symbol.intern("abc"));
            Keyword k2 = Keyword.intern(Symbol.intern("ab"));

            IDictionary dict = new Hashtable();
            dict[k1] = 7;
            dict["abc"] = 8;

            Expect(k1.invoke(dict), EqualTo(7));
            Expect(k2.invoke(dict), Null);
        }

        [Test]
        public void Invoke3IndexesIntoItsFirstArg()
        {
            Keyword k1 = Keyword.intern(Symbol.intern("abc"));
            Keyword k2 = Keyword.intern(Symbol.intern("ab"));

            IDictionary dict = new Hashtable();
            dict[k1] = 7;
            dict["abc"] = 8;

            Expect(k1.invoke(dict, 20), EqualTo(7));
            Expect(k2.invoke(dict, 20), EqualTo(20));
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void InvokeOnNoArgsFails()
        {
            Keyword k1 = Keyword.intern(Symbol.intern("abc"));
            object o = k1.invoke();
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void InvokeOnTooManyArgsFails()
        {
            Keyword k1 = Keyword.intern(Symbol.intern("abc"));
            IDictionary dict = new Hashtable();
            dict[k1] = 7;
            dict["abc"] = 8;

            object o = k1.invoke(dict, 20, null);
        }
  
        #endregion

        #region IComparable tests

        [Test]
        [ExpectedException(typeof(InvalidCastException))]
        public void CompareToNonKeywordFails()
        {
            Symbol s1 = Symbol.intern("abc");
            Keyword k1 = Keyword.intern(s1);
            int c = k1.CompareTo(s1);
        }

        [Test]
        public void CompareToEqualKeywordIsZero()
        {
            Keyword k1 = Keyword.intern(Symbol.intern("abc"));
            Keyword k2 = Keyword.intern(Symbol.intern("abc"));

            Expect(k1.CompareTo(k2), EqualTo(0));
        }

        [Test]
        public void CompareDependsOnSymbolCompare()
        {
            Symbol sym1 = Symbol.intern("abc");
            Symbol sym2 = Symbol.intern("a", "abc");
            Symbol sym3 = Symbol.intern("b", "abc");
            Keyword k1 = Keyword.intern(sym1);
            Keyword k2 = Keyword.intern(sym2);
            Keyword k3 = Keyword.intern(sym3);

            Expect(k1.CompareTo(k2), LessThan(0));
            Expect(k2.CompareTo(k1), GreaterThan(0));
            Expect(k1.CompareTo(k3), LessThan(0));
            Expect(k3.CompareTo(k1), GreaterThan(0));
        }


        #endregion
    }

}