Using Object.defineProperty to hide attributes from list of enumerable properties
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
19 * To change this license header, choose License Headers in Project Properties.
20 * To change this template file, choose Tools | Templates
21 * and open the template in the editor.
24 package org.apidesign.vm4brwsr;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import static org.testng.Assert.*;
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public class SizeOfAMethodTest {
38 private static String code;
40 @Test public void sumXYShouldBeSmall() {
42 int beg = s.indexOf("c.sum__III");
43 int end = s.indexOf("c.sum__III.access");
45 assertTrue(beg > 0, "Found sum method in " + code);
46 assertTrue(beg < end, "Found end of sum method in " + code);
48 String method = s.substring(beg, end);
51 assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
54 @Test public void betterConstructor() {
56 int beg = s.indexOf("c.initInflater__IIZ");
57 int end = s.indexOf("c.initInflater__IIZ.access");
59 assertTrue(beg > 0, "Found initInflater method in " + code);
60 assertTrue(beg < end, "Found end of initInflater method in " + code);
62 String method = s.substring(beg, end);
64 assertEquals(method.indexOf("stA1"), -1, "No need for stA1 register:\n" + method);
67 @Test public void deepConstructor() {
69 int beg = s.indexOf("c.intHolder__I");
70 int end = s.indexOf("c.intHolder__I.access");
72 assertTrue(beg > 0, "Found intHolder method in " + code);
73 assertTrue(beg < end, "Found end of intHolder method in " + code);
75 String method = s.substring(beg, end);
77 assertEquals(method.indexOf("stA3"), -1, "No need for stA3 register on second constructor:\n" + method);
80 @Test public void emptyConstructorRequiresNoStack() {
82 int beg = s.indexOf("CLS.cons__V");
83 int end = s.indexOf("CLS.cons__V.access");
85 assertTrue(beg > 0, "Found constructor in " + code);
86 assertTrue(beg < end, "Found end of constructor in " + code);
88 String method = s.substring(beg, end);
89 method = method.replace("constructor", "CNSTR");
91 assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
92 assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
95 @Test public void dontGeneratePrimitiveFinalConstants() {
96 assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
100 public static void compileTheCode() throws Exception {
101 final String res = "org/apidesign/vm4brwsr/StaticMethod";
102 StringBuilder sb = new StringBuilder();
103 class JustStaticMethod implements Bck2Brwsr.Resources {
105 public InputStream get(String resource) throws IOException {
106 final String cn = res + ".class";
107 if (resource.equals(cn)) {
108 return getClass().getClassLoader().getResourceAsStream(cn);
113 Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
114 code = sb.toString();
117 public static void releaseTheCode() {