// @(#) $Id$

package net.happygiraffe.jslint;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

/**
 * @author dom
 * @version $Id$
 */
public class JSLintTest {

    private JSLint lint;

    @Before
    public void setUp() throws IOException {
        lint = new JSLint();
    }

    // small helper function.
    private List<Issue> lint(Reader reader) throws IOException {
        return lint.lint("-", reader);
    }

    // small helper function.
    private List<Issue> lint(String source) {
        return lint.lint("-", source);
    }

    @Test
    public void testEmptySource() throws Exception {
        List<Issue> issues = lint("");
        assertThat(issues, is(not(nullValue())));
        assertThat(issues.size(), is(0));
    }

    @Test
    public void testLintReader() throws Exception {
        Reader reader = new StringReader("var foo = 1");
        List<Issue> issues = lint(reader);
        assertThat(issues, is(not(nullValue())));
        assertThat(issues.size(), is(1));
        assertThat(issues.get(0).getReason(), is("Missing semicolon."));
    }

    @Test
    public void testNoProblems() throws IOException {
        List<Issue> problems = lint("var foo = 1;");
        assertThat(problems, is(not(nullValue())));
        assertThat(problems.size(), is(0));
    }

    @Test
    public void testNullSource() throws Exception {
        List<Issue> issues = lint((String) null);
        assertThat(issues, is(not(nullValue())));
        assertThat(issues.size(), is(0));
    }

    @Test
    public void testOneProblem() throws IOException {
        // There is a missing semicolon here.
        List<Issue> problems = lint("var foo = 1");
        assertThat(problems, is(not(nullValue())));
        assertThat(problems.size(), is(1));
        assertThat(problems.get(0).getReason(), is("Missing semicolon."));
    }

    @Test
    public void testReportErrorsOnly() throws Exception {
        String html = lint.report("var foo = 42", true);
        assertThat(html, containsString("<div id=errors"));
        assertThat(html, containsString("Missing semicolon"));
    }

    @Test
    public void testReportFull() throws Exception {
        String html = lint.report("var foo = 42;");
        assertThat(html, containsString("<div>"));
    }

    @Test
    public void testResetOptions() throws Exception {
        String eval_js = "eval('1');";
        lint.addOption(Option.EVIL);
        lint.resetOptions();
        List<Issue> issues = lint(eval_js);
        assertThat(issues, is(not(nullValue())));
        assertThat(issues.size(), is(1));
        assertThat(issues.get(0).getReason(), is("eval is evil."));
    }

    @Test
    public void testSetOption() throws Exception {
        String eval_js = "eval('1');";
        // should be disallowed by default.
        List<Issue> issues = lint(eval_js);
        assertThat("evil disallowed", issues.size(), is(1));
        // Now should be a problem.
        lint.addOption(Option.EVIL);
        issues = lint(eval_js);
        assertThat("evil allowed", issues.size(), is(0));
    }

    @Test
    public void testSetOptionWithArgument() throws Exception {
        // This should only pass when indent=2.
        String js = "var x = 0;\nif (true) {\n  x = 1;\n}";
        lint.addOption(Option.WHITE);
        lint.addOption(Option.INDENT, 2);
        List<Issue> issues = lint(js);
        System.out.println(issues);
        assertThat(issues.size(), is(0));
    }

    // http://code.google.com/p/jslint4java/issues/detail?id=1
    @Test
    public void testUnableToContinue() throws Exception {
        // This isn't the originally reported problem, but it tickles the
        // "can't continue" message.
        List<Issue> issues = lint("\"");
        assertThat(issues.size(), is(2));
        assertThat(issues.get(0).getReason(), is("Unclosed string."));
        assertThat(issues.get(1).getReason(),
                is("Stopping, unable to continue. (0% scanned)."));
    }
}

@@ -3607,44 +3607,8 @@
 s);%0A
-        System.out.println(issues);%0A
     

Remove debug println().