package lukazitnik.jshint;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import org.junit.Assert;
import org.junit.Test;
import org.netbeans.junit.NbModuleSuite;
import org.netbeans.junit.NbTestCase;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;

public class JSHintTest extends NbTestCase {

    public JSHintTest(String name) {
        super(name);
    }

    public static junit.framework.Test suite() {
        return NbModuleSuite.create(JSHintTest.class, null, null);
    }

    @Test
    public void testLint() throws IOException {
        FileSystem fs = FileUtil.createMemoryFileSystem();
        FileObject fo = fs.getRoot().createData("index.js");
        PrintWriter out = (new PrintWriter(fo.getOutputStream()));
        out.write("a;");
        out.close();

        JSHint jshint = JSHint.getInstance();
        LinkedList<JSHintError> errors = jshint.lint(fo);
        JSHintError head = errors.element();

        Assert.assertEquals(1, errors.size());
        Assert.assertTrue(1 == head.getLine());
        Assert.assertEquals("Expected an assignment or function call and instead saw an expression.", head.getReason());
    }

    @Test
    public void testFindFile() throws IOException {
        FileSystem fs = FileUtil.createMemoryFileSystem();
        FileObject hasFile = fs.getRoot().createFolder("hasFile");
        FileObject file = hasFile.createData("file");
        FileObject childFolder = hasFile.createFolder("childFolder");

        FileObject result = JSHint.findFile("file", childFolder);
        Assert.assertEquals(file, result);
    }

    @Test
    public void testLintWithConfig() throws IOException {
        FileSystem fs = FileUtil.createMemoryFileSystem();

        FileObject jsFo = fs.getRoot().createData("index.js");
        PrintWriter jsOut = (new PrintWriter(jsFo.getOutputStream()));
        jsOut.write("while (day)\n  shuffle();");
        jsOut.close();

        FileObject configFo = fs.getRoot().createData(".jshintrc");
        PrintWriter configOut = (new PrintWriter(configFo.getOutputStream()));
        configOut.write("{\"curly\":true,\"undef\":true}");
        configOut.close();

        JSHint jshint = JSHint.getInstance();
        LinkedList<JSHintError> errors = jshint.lint(jsFo);

        Assert.assertEquals(3, errors.size());
        Assert.assertEquals("'shuffle' is not defined.", errors.pop().getReason());
        Assert.assertEquals("'day' is not defined.", errors.pop().getReason());
        Assert.assertEquals("Expected '{' and instead saw 'shuffle'.", errors.pop().getReason());
    }

}

@@ -832,32 +832,33 @@
 tputStream()));%0A
+%0A
         out.writ
@@ -1573,17 +1573,16 @@
 lder%22);%0A
-%0A
         
@@ -1639,16 +1639,17 @@
 older);%0A
+%0A
         
@@ -1766,33 +1766,35 @@
         File
-System fs
+Object root
  = FileUtil.
@@ -1817,18 +1817,27 @@
 System()
+.getRoot()
 ;
-%0A
 %0A       
@@ -1851,36 +1851,28 @@
 ject jsFo = 
-fs.getR
+r
 oot
-()
 .createData(
@@ -1947,32 +1947,33 @@
 tputStream()));%0A
+%0A
         jsOut.wr
@@ -2056,36 +2056,28 @@
  configFo = 
-fs.getR
+r
 oot
-()
 .createData(
@@ -2165,24 +2165,25 @@
 Stream()));%0A
+%0A
         conf
@@ -2568,32 +2568,32 @@
 ).getReason());%0A
-
         Assert.a
@@ -2681,10 +2681,743 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testGlobalsOption() throws IOException %7B%0A        FileObject root = FileUtil.createMemoryFileSystem().getRoot();%0A        FileObject jsFo = root.createData(%22index.js%22);%0A        PrintWriter jsOut = (new PrintWriter(jsFo.getOutputStream()));%0A%0A        jsOut.write(%22a(); b = 1;%22);%0A        jsOut.close();%0A%0A        FileObject config = root.createData(%22.jshintrc%22);%0A        PrintWriter configOut = (new PrintWriter(config.getOutputStream()));%0A%0A        configOut.write(%22%7B%5C%22undef%5C%22:true,%5C%22globals%5C%22:%7B%5C%22a%5C%22:false,%5C%22b%5C%22:true%7D%7D%22);%0A        configOut.close();%0A%0A        JSHint jshint =  JSHint.getInstance();%0A        LinkedList%3CJSHintError%3E errors = jshint.lint(jsFo);%0A%0A        Assert.assertEquals(0, errors.size());%0A    %7D%0A%0A
 %7D%0A

add failing test