/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.css.compiler.passes;

import com.google.common.collect.Lists;
import com.google.common.css.compiler.ast.BackDoorNodeMutation;
import com.google.common.css.compiler.ast.CssBlockNode;
import com.google.common.css.compiler.ast.CssDeclarationNode;
import com.google.common.css.compiler.ast.CssNode;
import com.google.common.css.compiler.ast.CssPropertyNode;
import com.google.common.css.compiler.ast.CssRootNode;
import com.google.common.css.compiler.ast.CssRulesetNode;
import com.google.common.css.compiler.ast.CssSelectorNode;
import com.google.common.css.compiler.ast.CssTree;
import com.google.common.css.compiler.ast.MutatingVisitController;

import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;

import java.util.List;

/**
 * Unit tests for {@link SplitRulesetNodes}.
 * 
 * @author oana@google.com (Oana Florescu)
 */
public class SplitRulesetNodesTest extends TestCase {

  public void testRunPass() {
    IMocksControl controller = EasyMock.createStrictControl();
    MutatingVisitController visitController = controller.createMock(
        MutatingVisitController.class);
    SplitRulesetNodes pass = new SplitRulesetNodes(visitController);
    visitController.startVisit(pass);
    controller.replay();

    pass.runPass();
    controller.verify();
  }

  public void testEnterRulesetNode() {
    IMocksControl controller = EasyMock.createStrictControl();
    MutatingVisitController visitController = controller.createMock(
        MutatingVisitController.class);
    SplitRulesetNodes pass = new SplitRulesetNodes(visitController);

    CssRulesetNode node = new CssRulesetNode();
    List<CssNode> replacementNodes = Lists.newArrayList();
    
    visitController.replaceCurrentBlockChildWith(replacementNodes, false);
    controller.replay();

    pass.enterRuleset(node);
    controller.verify();
  }

  public void testPassResult() {
    CssPropertyNode prop1 = new CssPropertyNode("padding", null);
    CssPropertyNode prop2 = new CssPropertyNode("color", null);

    CssDeclarationNode decl1 = new CssDeclarationNode(prop1);
    CssDeclarationNode decl2 = new CssDeclarationNode(prop2);
    
    CssRulesetNode ruleset = new CssRulesetNode();
    CssSelectorNode sel = new CssSelectorNode("foo", null);
    ruleset.addSelector(sel);
    ruleset.addDeclaration(decl1);
    ruleset.addDeclaration(decl2);
    
    CssBlockNode body = new CssBlockNode(false);
    BackDoorNodeMutation.addChildToBack(body, ruleset);
    
    CssRootNode root = new CssRootNode(body);
    CssTree tree = new CssTree(null, root);
    
    List<CssNode> replacementNodes = Lists.newArrayList();
    CssRulesetNode rule = new CssRulesetNode();
    rule.addDeclaration(decl1);
    rule.addSelector(sel);
    replacementNodes.add(rule);
    rule = new CssRulesetNode();
    rule.addDeclaration(decl1);
    rule.addSelector(sel);
    replacementNodes.add(rule);
    
    SplitRulesetNodes pass = new SplitRulesetNodes(
        tree.getMutatingVisitController());

    pass.runPass();
    assertEquals(tree.getRoot().getBody().toString(), 
        "[[foo]{[padding:[]]}, [foo]{[color:[]]}]");

    assertTrue("Nodes shouldn't appear twice in AST!",
        getFirstSelectorByRuleIndex(tree, 0)
        != getFirstSelectorByRuleIndex(tree, 1));
  }

  private CssSelectorNode getFirstSelectorByRuleIndex(CssTree tree, int index) {
    CssRulesetNode rule =
        (CssRulesetNode) tree.getRoot().getBody().getChildren().get(index);
    return rule.getSelectors().getChildren().get(0);
  }
}

@@ -637,16 +637,113 @@
 asses;%0A%0A
+import static com.google.common.truth.Truth.assertThat;%0Aimport static org.mockito.Mockito.mock;%0A%0A
 import c
@@ -1356,25 +1356,24 @@
 roller;%0A
-%0A
 import j
 unit.fra
@@ -1368,121 +1368,110 @@
 rt j
-unit.framework.TestCase;%0A%0Aimport org.easymock.EasyMock;%0Aimport org.easymock.IMocksControl;%0A%0Aimport java.util.List
+ava.util.List;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4
 ;%0A%0A/
@@ -1520,17 +1520,16 @@
 des%7D.%0A *
- 
 %0A * @aut
@@ -1568,16 +1568,39 @@
 cu)%0A */%0A
+@RunWith(JUnit4.class)%0A
 public c
@@ -1630,27 +1630,18 @@
 est 
-extends TestCase %7B%0A
+%7B%0A%0A  @Test
 %0A  p
@@ -1671,71 +1671,8 @@
 ) %7B%0A
-    IMocksControl controller = EasyMock.createStrictControl();%0A
     
@@ -1717,39 +1717,13 @@
 r = 
-controller.createMock(%0A        
+mock(
 Muta
@@ -1861,185 +1861,80 @@
 s);%0A
-    controller.replay();%0A%0A    pass.runPass();%0A    controller.verify();%0A  %7D%0A%0A  public void testEnterRulesetNode() %7B%0A    IMocksControl controller = EasyMock.createStrictControl();
+%0A    pass.runPass();%0A  %7D%0A%0A  @Test%0A  public void testEnterRulesetNode() %7B
 %0A   
@@ -1980,39 +1980,13 @@
 r = 
-controller.createMock(%0A        
+mock(
 Muta
@@ -2273,33 +2273,8 @@
 se);
-%0A    controller.replay();
 %0A%0A  
@@ -2306,35 +2306,18 @@
 ;%0A  
-  controller.verify();%0A  %7D%0A
+%7D%0A%0A  @Test
 %0A  p
@@ -3485,14 +3485,12 @@
 sert
-Equals
+That
 (tre
@@ -3521,18 +3521,17 @@
 String()
-, 
+)
 %0A       
@@ -3531,16 +3531,27 @@
         
+.isEqualTo(
 %22%5B%5Bfoo%5D%7B
@@ -3603,60 +3603,12 @@
 ertT
-rue(%22Nodes shouldn't appear twice in AST!%22,%0A        
+hat(
 getF
@@ -3639,16 +3639,17 @@
 tree, 0)
+)
 %0A       
@@ -3653,11 +3653,22 @@
     
-!= 
+.isNotEqualTo(
 getF

Migrate SplitRuleSetNodesTest to JUnit4 and Mockito