

package com.google.javascript.jscomp;

import com.google.javascript.jscomp.CompilerOptions.LanguageMode;

/**
 * @author mkretzschmar@google.com (Martin Kretzschmar)
 */
public final class CheckMissingGetCssNameTest extends CompilerTestCase {
  @Override
  protected CompilerPass getProcessor(final Compiler compiler) {
    return new CombinedCompilerPass(
        compiler,
        new CheckMissingGetCssName(compiler, CheckLevel.ERROR, "goog-[a-z-]*"));
  }

  public void testMissingGetCssName() {
    testMissing("var s = 'goog-inline-block'");
    testMissing("var s = 'CSS_FOO goog-menu'");
    testMissing("alert('goog-inline-block ' + goog.getClassName('CSS_FOO'))");
    testMissing("html = '<div class=\"goog-special-thing\">Hello</div>'");
  }

  public void testRecognizeGetCssName() {
    testNotMissing("var s = goog.getCssName('goog-inline-block')");
  }

  public void testIgnoreGetUniqueIdArguments() {
    testNotMissing("var s = goog.events.getUniqueId('goog-some-event')");
    testNotMissing("var s = joe.random.getUniqueId('joe-is-a-goob')");
  }

  public void testIgnoreAssignmentsToIdConstant() {
    testNotMissing("SOME_ID = 'goog-some-id'");
    testNotMissing("SOME_PRIVATE_ID_ = 'goog-some-id'");
    testNotMissing("var SOME_ID_ = 'goog-some-id'");
  }

  public void testNotMissingGetCssName() {
    testNotMissing("s = 'not-a-css-name'");
    testNotMissing("s = 'notagoog-css-name'");
  }

  public void testDontCrashIfTheresNoQualifiedName() {
    testMissing("things[2].DONT_CARE_ABOUT_THIS_KIND_OF_ID = "
                + "'goog-inline-block'");
    testMissing("objects[3].doSomething('goog-inline-block')");
  }

  public void testLetAndConstLefthandsides() {
    setAcceptedLanguage(LanguageMode.ECMASCRIPT6);
    testMissing("let s = 'goog-templit-css-name'");
    testMissing("const s = 'goog-templit-css-name'");
    testNotMissing("let s_ID = 'goog-templit-css-name'");
    testNotMissing("const s_ID = 'goog-templit-css-name'");
    testNotMissing("let s_ID_ = 'goog-templit-css-name'");
    testNotMissing("const s_ID_ = 'goog-templit-css-name'");
  }

  public void testIgnoreTemplateStrings() {
    setAcceptedLanguage(LanguageMode.ECMASCRIPT6);

    testMissing("var s = `goog-templit-css-name`");
    testNotMissing("var s = `goog-templit-css-name-${number}`");
    testNotMissing("var s = `not-a-css-name`");

    testNotMissing("var s = `not-CSS${sub}gsoog-templit`");

    testNotMissing("var s = goog.events.getUniqueId(`goog-a-css-name`)");
    testNotMissing("var s = goog.getCssName(`goog-a-css-name`)");

    testMissing("let s = `goog-templit-css-name`");
    testNotMissing("const SOME_ID = `goog-some-id`");
    testMissing("const s = `goog-some-id`");
  }

  private void testMissing(String js) {
    testError(js, CheckMissingGetCssName.MISSING_GETCSSNAME);
  }

  private void testNotMissing(String js) {
    test(js, js);
  }
}
