map.js revision 2:da1e581c933b
1171831Skan/*
2171831Skan * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
3171831Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4171831Skan *
5169695Skan * This code is free software; you can redistribute it and/or modify it
6169695Skan * under the terms of the GNU General Public License version 2 only, as
7169695Skan * published by the Free Software Foundation.
8169695Skan *
9169695Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10169695Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11169695Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12169695Skan * version 2 for more details (a copy is included in the LICENSE file that
13169695Skan * accompanied this code).
14169695Skan *
15169695Skan * You should have received a copy of the GNU General Public License version
16169695Skan * 2 along with this work; if not, write to the Free Software Foundation,
17169695Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169695Skan *
19169695Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169695Skan * or visit www.oracle.com if you need additional information or have any
21169695Skan * questions.
22169695Skan */
23169695Skan
24169695Skan/**
25169695Skan * Tests for java.util.Map behavior in Nashorn
26169695Skan *
27169695Skan * @test
28169695Skan * @run
29169695Skan */
30169695Skanvar m = new (Java.type("java.util.LinkedHashMap"));
31169695Skanprint("m.class.name=" + m.class.name) // Has "class" property like any POJO
32169695Skan
33169695Skanvar empty_key = "empty"
34169695Skan
35169695Skanprint("m = " + m)
36169695Skanprint("m.empty = " + m.empty) // prints "true"
37169695Skanprint("m['empty'] = " + m['empty']) // prints "true"; item not found, default to property
38169695Skanprint("m[empty_key] = " + m[empty_key]) // prints "true"; item not found, default to property
39169695Skan
40169695Skanm.put("empty", "foo")
41169695Skan
42169695Skanprint("m = " + m)
43169695Skanprint("m.empty = " + m.empty) // prints "false"
44169695Skanprint("m['empty'] = " + m['empty'])
45169695Skanprint("m[empty_key] = " + m[empty_key]) // prints "foo"
46169695Skan
47169695Skanprint("m.bwah = " + m.bwah) // prints "null"
48169695Skanprint("m['bwah'] = " + m['bwah']) // prints "null"
49169695Skan
50169695Skanm.put("twonk", "ding")
51169695Skanprint("m.twonk = " + m.twonk) // prints "ding"
52169695Skanprint("m['twonk'] = " + m['twonk']) // prints "ding"
53169695Skan
54169695Skanprint("m.size()=" + m.size())
55169695Skan
56169695Skanprint("--for each begin--")
57169695Skanfor each (i in m.keySet()) {
58169695Skan  print(i)
59169695Skan}
60169695Skanprint("--for each end--")
61169695Skan