/**
* This class implements TestSet and the purpose of this class to test
* BusinessCalendar For Timers. The results of the test are recorded using
* TestRecorder class.
*/
package com.fujitsu.iflowqa.testframe;

import com.fujitsu.iflow.common.JSConversions;

public class ConversionTests implements TestSet
{
    private TestRecorder tr;

    public
    void
    runTests(
        TestRecorder ntr)
        throws Exception
    {
        tr = ntr;
        testJavaScriptConversion("",         "\"\"",           "Empty String");
        testJavaScriptConversion("abc",      "\"abc\"",        "Simple string with no special characters");
        testJavaScriptConversion("abc\\def", "\"abc\\\\def\"", "Slash in the middle");
        testJavaScriptConversion("\\def",    "\"\\\\def\"",    "Slash in the beginning");
        testJavaScriptConversion("abc\\",    "\"abc\\\\\"",    "Slash in the end");
        testJavaScriptConversion("abc\"def", "\"abc\\\"def\"", "Quote in the middle");
        testJavaScriptConversion("\"def",    "\"\\\"def\"",    "Quote in the beginning");
        testJavaScriptConversion("abc\"",    "\"abc\\\"\"",    "Quote in the end");
        testJavaScriptConversion("abc\ndef", "\"abc\\ndef\"",  "Return in the middle");
        testJavaScriptConversion("\ndef",    "\"\\ndef\"",     "Return in the beginning");
        testJavaScriptConversion("abc\n",    "\"abc\\n\"",     "Return in the end");
        testJavaScriptConversion("\"",       "\"\\\"\"",       "only a double quote");
        testJavaScriptConversion("\\",       "\"\\\\\"",       "Only a slash");
        testJavaScriptConversion("\n",       "\"\\n\"",        "Only a return");
        testJavaScriptConversion("\n",       "\"\\n\"",        "Only a return");
        testJavaScriptConversion("1\"2\\3\n4\r5\t6\f7\b8",
                                             "\"1\\\"2\\\\3\\n4\\r5\\t6\\f7\\b8\"",
                                                               "all of the allowed special characters");
    }

    private
    void
    testJavaScriptConversion(
        String strVal,
        String literal,
        String comment)
        throws Exception
    {
        StringBuffer sb = new StringBuffer();
        JSConversions.quote4JS(sb, strVal);
        if (literal.equals(sb.toString())) {
            tr.markPassed(comment+" (to Literal)");
        }
        else {
            tr.markFailed(comment+" (to Literal)", "Expected ["+literal+"] but got ["+sb.toString()+"] instead" );
        }
        sb.setLength(0);
        JSConversions.unquote4JS(sb, literal);
        if (strVal.equals(sb.toString())) {
            tr.markPassed(comment+" (from Literal)");
        }
        else {
            tr.markFailed(comment+" (from Literal)", "Expected ["+literal+"] but got ["+sb.toString()+"] instead" );
        }
    }

    /**
    * TestDriver is the class that knows how to run these tests.
    * So just call it with the name of this class in there.
    */
    public
    static
    void
    main(String[] args) {
        String[] newArgs = new String[1];
        newArgs[0] = "com.fujitsu.iflowqa.testframe.ConversionTests";
        TestDriver.main(newArgs);
    }

}
