import pprint
from patterny.nlp.txt import pre_process
from nose.tools import assert_not_equal


class TestTxt(object):
    """
        This is a dummy test. It only checks whether the output is None
        To see how the output looks like, run the test on the root folder like this:
            nosetests --nocapture tests/test_nlp_txt.py > out/pre_process.txt
        Then, check the pre_process.txt output file
    """

    def setup(self):
        self.pp = pprint.PrettyPrinter(indent=2)

    def teardown(self):
        pass

    def test_pre_process(self):
        txt_separator = '\n\n--------------------------\n\n'

        data = """
            Created attachment 290817 [details] [diff] [review]
            A little patch for MinGW gcc compiling.

            When compile Mozilla Firefox trunk source code with MinGW gcc 3.4.5, the compiler complain for the subtraction operation between unsigned char* and char* operand. So, I just convert them to unsigned int before subtraction.

            A little patch but make the MinGW buildable.
        """
        out = pre_process(data)
        print txt_separator
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Margaret Leibovic [:margaret] from comment #2)

            > @@ +6390,5 @@
            > > +	    return;
            > > +	options.contentWindow = null;
            > > +	options.sourceURI = null;
            > > +    };
            > > +
            >
            > You're not using any of these three objects, so I'm not sure why you're
            > setting them. Was this just a copy/paste error?

            Grr, splinter review cut off what I was trying to comment on. I'm talking about these three things:

            +    options.contentWindow = browser.contentWindow;
            +    options.sourceURI = browser.currentURI;
            +    options.eventCallback = function(aEvent) {
            +    	if (aEvent != "removed")
            +	    return;
            +	options.contentWindow = null;
            +	options.sourceURI = null;
            +    };
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Created attachment 571937 [details]
            BugID242624-FF.noleftmargin.game.jpg

            User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2

            Steps to reproduce:

            Log in to Facebook.
            Profile page displays
            Go to "Home"
            Select game from left frame.


            Actual results:

            No left margin -- image bleeds to left edge.

            Note:  This problem does not appear in Firefox 7.x or in Safari 5.0.6
            Exists in Beta (Y/N):  Y
            Exists in Aurora (Y/N):  Y
            Exists in Nightly (Y/N): Y
            Does it occur in safe mode?  Y
            Does it occur with a clean profile? Y
            OS:  Mac OS X 10.5.8


            Expected results:

            Left margin for profile pic and for games
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)


        data = """
            User-Agent:       Mozilla/5.0 (X11; U; Linux i686; tr-TR; rv:1.8.1.3) Gecko/20070223 Pardus/2007 Firefox/2.0.0.3
            Build Identifier: Mozilla/5.0 (X11; U; Linux i686; tr-TR; rv:1.8.1.3) Gecko/20070223 Pardus/2007 Firefox/2.0.0.3

            When using Cisco VPN Client to access the internet over a secured Wireless VPN tunnel, i can't log in to my gmail account with firefox browser. Problem occurs on different linux distributions (ubuntu, pardus, etc.) but not on Windows. Using Windows, i can log in to my gmail account with firefox over a VPN connection.

            Reproducible: Always

            Steps to Reproduce:
            1.Establish a vpn connection with vpn client
            2.Enter www.gmail.com and provide your username/password then click 'Log In'
            3.A Blank page loads. Refreshing doesn't help to fix to login process.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5
            Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5

            Open the page
            http://www.aztv.co.il/site/programs/programs.asp?start=now
            and you get an empty diplay panel, that should contain the list channels and the programs that are currently being broadcasted.

            Something is wrong with the java script - either on the page or in the mozilla interpreter.
            (IE displayes it just fine)

            Reproducible: Always
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            User-Agent:       Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
            Build Identifier: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0

            On several pages, with a short variety of different HTML elements, I have noticed that text in these elements overlaps with the text to the right and/or left. This seems to occur most times that the deprecated &lt;b&gt; and &lt;i&gt; elements are used, and also seems to occur if the preferred &lt;em&gt; element is used in a non-standard form, for instance: &lt;em class=&quot;sound&quot;&gt;ahem&lt;/em&gt;. I have been able to temporarily alleviate these problems by modifying the style sheets to insert padding of 0.5em to the em element. Testing on my end has not reproduced this problem on the Windows or Linux builds of 2.0.
            I've noticed this since upgrading to v2.0.

            Reproducible: Always

            Steps to Reproduce:
            1. Visit a page with those HTML elements.
            Actual Results:
            Every time I visit a page with the deprecated italics tags, or with these emphasis tags, I am seeing the text of those elements overlapping the other text in their paragraphs.

            Expected Results:
            This text should not overlap. This makes it very difficult to read the content.
        """
        out = pre_process(data)
        assert_not_equal(out, None)
        print out
        print txt_separator

        data = "Reproduceable on \nMozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20030925 Firebird/0.7"
        out = pre_process(data)
        assert_not_equal(out, None)
        print out
        print txt_separator

        data = "WFM on Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)\n\nNot sure though if the site has been updated in the meantime."
        out = pre_process(data)
        assert_not_equal(out, None)
        print out
        print txt_separator

        data = "Reading comment 6 again I can also confirm that http://www.doctrine-project.org/ if WFM on Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115\nFirefox/3.6 (.NET CLR 3.5.30729)\n\nimho resolved."
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = "WFM with Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.3a1pre) Gecko/20100116 Minefield/3.7a1pre (.NET CLR 3.5.30729) ID:20100116042715. Still seeing this?"
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = "Reproduceable on \n"
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = "WFM on \n\nNot sure though if the site has been updated in the meantime."
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = "Reading comment 6 again I can also confirm that http://www.doctrine-project.org/ if WFM \nFirefox/3.6 (.NET CLR 3.5.30729)\n\nimho resolved."
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = "WFM with  Minefield/3.7a1pre (.NET CLR 3.5.30729) ID:20100116042715. Still seeing this?"
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            this test fails for me on both tegra and panda platforms:
            INFO | automation.py | Application pid: 0
            0 INFO SimpleTest START
            1 INFO TEST-START | testFindInPage
            2 INFO TEST-PASS | testFindInPage | Awesomebar URL typed properly - http://mochi.test:8888/tests/robocop/robocop_text_page.html should equal http://mochi.test:8888/tests/robocop/robocop_text_page.html
            3 INFO TEST-PASS | testFindInPage | Looking for the next search match button in the Find in Page UI - Found the next match button
            4 INFO TEST-PASS | testFindInPage | Checking if the next button was clicked - button was clicked
            5 INFO TEST-PASS | testFindInPage | Checking if the next button was clicked - button was clicked
            6 INFO TEST-UNEXPECTED-FAIL | testFindInPage | checking that painted surface loaded - painted surface loaded
            Exception caught during test!
            junit.framework.AssertionFailedError: 6 INFO TEST-UNEXPECTED-FAIL | testFindInPage | checking that painted surface loaded - painted surface loaded
                at junit.framework.Assert.fail(Assert.java:47)
                at org.mozilla.fennec_jmaher.FennecMochitestAssert._logMochitestResult(FennecMochitestAssert.java:107)
                at org.mozilla.fennec_jmaher.FennecMochitestAssert.ok(FennecMochitestAssert.java:136)
                at org.mozilla.fennec_jmaher.tests.PixelTest.waitForPaint(PixelTest.java:51)
                at org.mozilla.fennec_jmaher.tests.testFindInPage.testFindInPage(testFindInPage.java:28)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:521)
                at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
                at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
                at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
                at org.mozilla.fennec_jmaher.tests.BaseTest.runTest(BaseTest.java:130)
                at junit.framework.TestCase.runBare(TestCase.java:127)
                at junit.framework.TestResult$1.protect(TestResult.java:106)
                at junit.framework.TestResult.runProtected(TestResult.java:124)
                at junit.framework.TestResult.run(TestResult.java:109)
                at junit.framework.TestCase.run(TestCase.java:118)
                at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
                at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
                at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
                at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
            7 INFO TEST-UNEXPECTED-FAIL | testFindInPage | Exception caught - junit.framework.AssertionFailedError: 6 INFO TEST-UNEXPECTED-FAIL | testFindInPage | checking that painted surface loaded - painted surface loaded
            8 INFO TEST-END | testFindInPage | finished in 72053ms
            9 INFO TEST-START | Shutdown
            10 INFO Passed: 4
            11 INFO Failed: 2
            12 INFO Todo: 0
            13 INFO SimpleTest FINISHED
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            I think null BitmapDrawable is the problem. In the following bug's raw dump, GeckoJarReader logs a "file not found" error.

            https://crash-stats.mozilla.com/report/index/8ae4272a-6ee3-4ce7-bbb0-92cff2130226

            java.io.FileNotFoundException: /data/app/org.mozilla.firefox_beta-1.apk: open failed: ENOENT (No such file or directory)
               at libcore.io.IoBridge.open(IoBridge.java:416)
               at java.io.RandomAccessFile.<init>(RandomAccessFile.java:118)
               at java.io.RandomAccessFile.<init>(RandomAccessFile.java:150)
               at java.util.zip.ZipFile.<init>(ZipFile.java:130)
               at java.util.zip.ZipFile.<init>(ZipFile.java:103)
               at org.mozilla.gecko.util.GeckoJarReader.getZipFile(GeckoJarReader.java:104)
               at org.mozilla.gecko.util.GeckoJarReader.getBitmapDrawable(GeckoJarReader.java:39)
               at org.mozilla.gecko.Favicons$LoadFaviconTask.downloadFavicon(Favicons.java:252)
               at org.mozilla.gecko.Favicons$LoadFaviconTask.doInBackground(Favicons.java:325)
               at org.mozilla.gecko.Favicons$LoadFaviconTask.doInBackground(Favicons.java:207)
               at android.os.AsyncTask$2.call(AsyncTask.java:287)
               at java.util.concurrent.FutureTask.run(FutureTask.java:234)
               at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
               at java.lang.Thread.run(Thread.java:856)
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Wesley Johnston (:wesj) from comment #1)
            > (In reply to Brian Nicholson (:bnicholson) from comment #0)
            > > In general, I think it's a bad idea to have about:home fire off any network
            > > requests. It will slow down startup since the requests go through the same
            > > background thread we use for other tasks. And if there are any errors as
            > > outlined above, things will be even worse.
            >
            > To be fair, both these issues are solvable.

            Yes, and we could be smarter about NOT trying to re-download a favicon that we failed to get last time. Maybe use a timeout? Desktop does this too, IIRC.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Kartikaya Gupta (:kats) from comment #4)
            > > > +    public static final float THUMBNAIL_ASPECT_RATIO = 0.714f;
            > >
            > > Can you add a comment explaining where the 0.714 aspect ratio comes from?
            >
            > I have no idea where it comes from. I got it from the code in Tabs.java that
            > I deleted.

            :wesj, do you mind explaining where this comes from?
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Wesley Johnston (:wesj) from comment #1)
            > (In reply to Brian Nicholson (:bnicholson) from comment #0)
            > > In general, I think it's a bad idea to have about:home fire off any network
            > > requests. It will slow down startup since the requests go through the same
            > > background thread we use for other tasks. And if there are any errors as
            > > outlined above, things will be even worse.
            >
            > To be fair, both these issues are solvable.

            Both? The fact that everything goes through the same background thread, yes. But we don't have control over the multiple factors listed in comment 0 that could prevent the favicon from loading.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Tetsuharu OHZEKI [:saneyuki_s] from comment #4)
            > Created attachment 715592 [details] [diff] [review]
            > specify value types
            >
            > Backout bug 840722, or specify a value type.

            I'm leery of making a change like this without properly understanding the codepath that is causing the problem. I'll back out bug 840722.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            (In reply to Kartikaya Gupta (email:kats@mozilla.com) from comment #6)
            >
            > I'm leery of making a change like this without properly understanding the
            > codepath that is causing the problem. I'll back out bug 840722.

            OK. I understand.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Firefox crashes at leat 6 times a day.  Installed latest version but still crashing.  Goes very slow before it crashes.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Steps to reproduce:
            W have installed Firefox 18 (as we did with all previous version) on Solaris 10 SPAC 64b

            Actual results:
            When we tried to start it form a console, it crached with a message: Segmentation fault.
            And it produced a core dump

            Expected results:
            Firefox should have open correctly
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            If you are navigating through the list of open tabs inside the All+Tabs panel and you wanna filter by a term you have to select the search text field first.
            It would be nice if any entered character is automatically routed to the search field and the filter gets applied.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            In maximized mode there is something like 3 pixels padding on the right side of "All tabs" panel.
            It doesn't exist on the left side of panel and in not maximized mode.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            When you have the All+Tabs panel open it would be great if you can press Cmd/Ctrl+F to focus the search text field. Right now the panel gets hidden and the Find toolbar is shown without focus.
            IMO using the command inside the All+Tabs panel would make more sense.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Steps to reproduce:
            Nothing... had multiple windows and tiles open... for about 4 hours

            Actual results:
            Crashed without warning
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Firefox crashes at leat 6 times a day.  Installed latest version but still crashing.  Goes very slow before it crashes.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            Steps to reproduce:
            W have installed Firefox 18 (as we did with all previous version) on Solaris 10 SPAC 64b

            Actual results:
            When we tried to start it form a console, it crashed with a message: Segmentation fault.
            And it produced a core dump

            Expected results:
            Firefox should have open correctly
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            screen shots: Aurora 7.0a2 top, FF 3.6.18 bottom

            User Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:7.0a2) Gecko/20110709 Firefox/7.0a2
            Build ID: 20110709042004

            Steps to reproduce:

            Start Aurora with a new Profile just to be sure
            Open Menu Preferences
            Select  Advanced | Encryption , View Certificates
            Select tab certificates, scroll down to Thawte
            Select first Thawte Certificate, click "view..." button

            (Actually the problem is not specific to this single certificate. I tried many more built-in root certificates and they all had the same issue.)



            Actual results:

            Certificate Viewer opens, it says "could not verify this certificate for unknown reasons"


            Expected results:

            Certificate Viewer opens, it should say "this certificates has been verified for the following uses" and a list of uses.
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)

        data = """
            I agree with the utility of this feature it's just going to take some serious work. This is a pretty hacky area of the code that is screaming for a rewrite.

            I'll slate this for 1.1
        """
        out = pre_process(data)
        print out
        print txt_separator
        assert_not_equal(out, None)
