예제 #1
0
  /** Dumps messages to the given output stream, returning the highest message level seen. */
  static MessageLevel dumpMessages(MessageQueue mq, MessageContext mc, Appendable out) {
    MessageLevel maxLevel = MessageLevel.values()[0];
    for (Message m : mq.getMessages()) {
      MessageLevel level = m.getMessageLevel();
      if (maxLevel.compareTo(level) < 0) {
        maxLevel = level;
      }
    }
    MessageLevel ignoreLevel = null;
    if (maxLevel.compareTo(MessageLevel.LINT) < 0) {
      // If there's only checkpoints, be quiet.
      ignoreLevel = MessageLevel.LOG;
    }
    try {
      for (Message m : mq.getMessages()) {
        MessageLevel level = m.getMessageLevel();
        if (ignoreLevel != null && level.compareTo(ignoreLevel) <= 0) {
          continue;
        }
        out.append(level.name() + ": ");
        m.format(mc, out);
        out.append("\n");

        if (maxLevel.compareTo(level) < 0) {
          maxLevel = level;
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return maxLevel;
  }
예제 #2
0
파일: Jobs.java 프로젝트: sahityat/caja
 public boolean hasNoMessagesOfLevel(MessageLevel level) {
   for (Message m : getMessageQueue().getMessages()) {
     if (level.compareTo(m.getMessageLevel()) <= 0) {
       return false;
     }
   }
   return true;
 }
예제 #3
0
 protected boolean containsConsistentMessage(List<Message> list, MessageTypeInt type) {
   for (Message m : list) {
     System.out.println("**" + m.getMessageType() + "|" + m.getMessageLevel());
     if (m.getMessageType().equals(type)) {
       return true;
     }
   }
   return false;
 }
예제 #4
0
  private void runCajoled(String js, Object golden, String context) throws Exception {
    Block uncajoledModuleBody = js(fromString(js));
    System.err.println("\n\nblock\n=====\n" + uncajoledModuleBody.toStringDeep(1));

    PluginMeta meta = new PluginMeta();
    meta.setDebugMode(true);

    Jobs jobs = new Jobs(mc, mq, meta);
    jobs.getJobs().add(new Job(AncestorChain.instance(uncajoledModuleBody)));

    Pipeline<Jobs> pipeline = new Pipeline<Jobs>();
    pipeline.getStages().add(new ConsolidateCodeStage());
    pipeline.getStages().add(new ValidateJavascriptStage(new TestBuildInfo()));
    pipeline.getStages().add(new InferFilePositionsStage());
    pipeline.getStages().add(new DebuggingSymbolsStage());
    if (!pipeline.apply(jobs)) {
      StringBuilder sb = new StringBuilder();
      for (Message msg : mq.getMessages()) {
        if (0 == sb.length()) {
          sb.append('\n');
        }
        sb.append(msg.getMessageLevel()).append(": ");
        msg.format(mc, sb);
      }
      fail(sb.toString());
    }

    CajoledModule cajoledModule = jobs.getJobs().get(0).getRoot().cast(CajoledModule.class).node;

    try {
      String cajoledText = String.format(context, render(cajoledModule));
      Object actual =
          RhinoTestBed.runJs(
              new RhinoTestBed.Input(getClass(), "/js/json_sans_eval/json_sans_eval.js"),
              new RhinoTestBed.Input(getClass(), "../console-stubs.js"),
              new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita.js"),
              new RhinoTestBed.Input(getClass(), "/com/google/caja/log-to-console.js"),
              new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita-debugmode.js"),
              new RhinoTestBed.Input(cajoledText, getName()));
      assertEquals(golden, actual);
    } catch (Exception ex) {
      System.err.println(render(cajoledModule));
      throw ex;
    } catch (Error ex) {
      System.err.println(render(cajoledModule));
      throw ex;
    }
  }
예제 #5
0
  // TODO(ihab.awad): Refactor tests to use checkAddsMessage(...) instead
  protected void checkFails(String input, String error) throws Exception {
    mq.getMessages().clear();
    getRewriter()
        .expand(
            new UncajoledModule(
                new Block(FilePosition.UNKNOWN, Arrays.asList(js(fromString(input, is))))));

    assertFalse("Expected error, found none: " + error, mq.getMessages().isEmpty());

    StringBuilder messageText = new StringBuilder();
    Message firstError = null;
    for (Message m : mq.getMessages()) {
      if (m.getMessageLevel().compareTo(MessageLevel.WARNING) >= 0) {
        firstError = m;
      }
    }
    firstError.format(mc, messageText);
    assertTrue(
        "First error is not \"" + error + "\": " + messageText.toString(),
        messageText.toString().contains(error));
  }
예제 #6
0
 protected void checkSucceeds(
     ParseTreeNode inputNode, ParseTreeNode expectedResultNode, MessageLevel highest) {
   mq.getMessages().clear();
   ParseTreeNode actualResultNode = getRewriter().expand(inputNode);
   for (Message m : mq.getMessages()) {
     if (m.getMessageLevel().compareTo(highest) >= 0) {
       fail(m.toString());
     }
   }
   if (expectedResultNode != null) {
     // Test that the source code-like renderings are identical. This
     // will catch any obvious differences between expected and
     // actual.
     assertEquals(render(expectedResultNode), render(actualResultNode));
     // Then, for good measure, test that the S-expression-like
     // formatted representations are also identical. This will catch
     // any differences in tree topology that somehow do not appear
     // in the source code representation (usually due to programming
     // errors).
     assertEquals(TestUtil.format(expectedResultNode), TestUtil.format(actualResultNode));
   }
 }
  private void runTest(String goldenIhtml, String inputIhtml, Message... expectedMessages)
      throws Exception {
    Element ihtmlRoot =
        new DomParser(
                DomParser.makeTokenQueue(
                    FilePosition.startOfFile(is), new StringReader(inputIhtml), true, false),
                true,
                mq)
            .parseDocument();
    new IhtmlSanityChecker(mq).check(ihtmlRoot);

    for (Message msg : expectedMessages) {
      assertMessage(
          true,
          msg.getMessageType(),
          msg.getMessageLevel(),
          msg.getMessageParts().toArray(new MessagePart[0]));
    }
    assertMessagesLessSevereThan(MessageLevel.WARNING);

    String checkedIhtml = Nodes.render(ihtmlRoot, MarkupRenderMode.XML);
    assertEquals(goldenIhtml, checkedIhtml);
  }