@Test public void test() { /* * Read initial css code */ CssParser cssParserInputFile = new Css3Parser(); try { cssParserInputFile.parse(Config.getInstance().inputFile()); cssParserInputFile.removeDuplicates(); } catch (CssParsingException e) { IoUtils.printErrorAndExit(e); } /* * Generate sass code from initial css code */ Pair<List<SslMixin>, List<SslRuleset>> statements = null; try { statements = CssToSsl.compute(cssParserInputFile.getRulesets()); } catch (Exception e) { IoUtils.printErrorAndExit(e); } /* * Write generated sass code */ try { SslWriter sslWriter = new SassWriterDebug(); sslWriter.writeGeneratedCode(statements, Config.getInstance().outputFile()); } catch (IOException e) { IoUtils.printErrorAndExit(e); } /* * Generate css code from generated sass code */ try { ScssStylesheet stylesheet = ScssStylesheet.get(this.sassGeneratedFile.toString()); stylesheet.compile(); Files.write(this.cssGeneratedFile.toPath(), stylesheet.printState().getBytes()); } catch (Exception e) { IoUtils.printErrorAndExit(e); } /* * Read generated css code */ CssParser cssParserGeneratedFile = new Css3Parser(); try { cssParserGeneratedFile.parse(this.cssGeneratedFile); if (!Config.getInstance().noDuplicatesInRuleset()) { cssParserGeneratedFile.removeDuplicates(); } } catch (CssParsingException e) { IoUtils.printErrorAndExit(e); } /* * Compare initial css file and generated css file, * according to the configuration */ List<CssRuleset> initialRulesets = cssParserInputFile.getRulesets(); List<CssRuleset> generatedRulesets = cssParserGeneratedFile.getRulesets(); if (Config.getInstance().preserveSemantic()) { Consumer<List<CssRuleset>> rulesetsSorting = r -> { Collections.sort( r, (CssRuleset r1, CssRuleset r2) -> { Selector s1 = r1.getSelector(); Selector s2 = r2.getSelector(); int firstRulesetPosition = s1.getPosition().getLineNumber(); int secondRulesetPosition = s2.getPosition().getLineNumber(); if (firstRulesetPosition < secondRulesetPosition) { return -1; } else if (firstRulesetPosition == secondRulesetPosition) { return s1.getSelector().compareTo(s2.getSelector()); } else { return 1; } }); }; assertEquals(initialRulesets.size(), generatedRulesets.size()); rulesetsSorting.accept(initialRulesets); rulesetsSorting.accept(generatedRulesets); for (int i = 0; i < initialRulesets.size(); i++) { CssRuleset initialRuleset = initialRulesets.get(i); CssRuleset generatedRuleset = generatedRulesets.get(i); String initialSelector = initialRuleset.getSelector().getSelector(); String generatedSelector = generatedRuleset.getSelector().getSelector(); List<DeclarationConcrete> initialDeclarations = initialRuleset.getDeclarations(); List<DeclarationConcrete> generatedDeclarations = generatedRuleset.getDeclarations(); assertEquals(initialSelector, generatedSelector); if (Config.getInstance().noDuplicatesInRuleset()) { assertEquals(initialDeclarations.size(), generatedDeclarations.size()); } assertEquals(Sets.newHashSet(initialDeclarations), Sets.newHashSet(generatedDeclarations)); } } else { Function<List<CssRuleset>, Map<String, List<DeclarationConcrete>>> mergeIdenticalSelectors = rulesets -> { Map<String, List<DeclarationConcrete>> result = new HashMap<>(); rulesets.forEach( ruleset -> { String selector = ruleset.getSelector().getSelector(); List<DeclarationConcrete> declarations = ruleset.getDeclarations(); if (!result.containsKey(selector)) { result.put(selector, Lists.newArrayList()); } result.get(selector).addAll(declarations); }); return result; }; Map<String, List<DeclarationConcrete>> initialData = mergeIdenticalSelectors.apply(initialRulesets); Map<String, List<DeclarationConcrete>> generatedData = mergeIdenticalSelectors.apply(generatedRulesets); assertEquals(initialData.keySet(), generatedData.keySet()); initialData.forEach( (initialSelector, initialDeclarations) -> { List<DeclarationConcrete> generatedDeclarations = generatedData.get(initialSelector); if (Config.getInstance().noDuplicatesInRuleset()) { assertEquals(initialDeclarations.size(), generatedDeclarations.size()); } assertEquals( Sets.newHashSet(initialDeclarations), Sets.newHashSet(generatedDeclarations)); }); } }
@Before public void before() { if (!this.config.checkConfig()) { IoUtils.exitOnError(); } }