/*
 Copyright 2013-2016 Jason Leyba
 
 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.github.jsdossier.jscomp;

import static com.github.jsdossier.testing.CompilerUtil.createSourceFile;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;

import com.github.jsdossier.annotations.Input;
import com.github.jsdossier.testing.CompilerUtil;
import com.github.jsdossier.testing.GuiceRule;
import java.nio.file.FileSystem;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ModuleCollectionPassTest {

  @Rule
  public GuiceRule guice = GuiceRule.builder(this).setUseNodeLibrary(false).build();

  @Inject
  @Input
  private FileSystem inputFs;
  @Inject private TypeRegistry typeRegistry;
  @Inject private CompilerUtil util;

  @Test
  public void identifiesClosureModules() {
    util.compile(
        createSourceFile(
            inputFs.getPath("module/foo.js"), 
            "goog.module('foo');",
            "exports.X = class {};"));
    Module module = getOnlyElement(typeRegistry.getAllModules());
    assertThat(module.getOriginalName()).isEqualTo("foo");
    assertThat(module.isClosure()).isTrue();
  }

  @Test
  public void identifiesEs6Modules() {
    util.compile(
        createSourceFile(
            inputFs.getPath("module/foo.js"),
            "export class X {}"));
    Module module = getOnlyElement(typeRegistry.getAllModules());
    assertThat(module.getOriginalName()).isEqualTo("module/foo.js");
    assertThat(module.isEs6()).isTrue();
  }

  @Test
  public void identifiesNodeModules() {
    defineInputModules("module", "foo.js");
    util.compile(
        createSourceFile(
            inputFs.getPath("module/foo.js"),
            "exports.X = class {};"));
    System.out.println(util.toSource());
    Module module = getOnlyElement(typeRegistry.getAllModules());
    assertThat(module.getOriginalName()).isEqualTo("module$module$foo");
    assertThat(module.isNode()).isTrue();
  }

  private void defineInputModules(String prefix, String... modules) {
    guice
        .toBuilder()
        .setModulePrefix(prefix)
        .setModules(modules)
        .build()
        .createInjector()
        .injectMembers(this);
  }
}

@@ -1,12 +1,11 @@
 /*%0A
- 
 Copyrigh
@@ -28,19 +28,17 @@
 n Leyba%0A
- %0A 
+%0A
 Licensed
@@ -93,17 +93,16 @@
 ense%22);%0A
- 
 you may 
@@ -158,17 +158,16 @@
 icense.%0A
- 
 You may 
@@ -198,19 +198,17 @@
 ense at%0A
- %0A 
+%0A
   http:/
@@ -244,19 +244,17 @@
 NSE-2.0%0A
- %0A 
+%0A
 Unless r
@@ -313,17 +313,16 @@
 oftware%0A
- 
 distribu
@@ -379,17 +379,16 @@
  BASIS,%0A
- 
 WITHOUT 
@@ -452,17 +452,16 @@
 mplied.%0A
- 
 See the 
@@ -520,17 +520,16 @@
 ons and%0A
- 
 limitati
@@ -551,17 +551,16 @@
 icense.%0A
- 
 */%0A%0Apack
@@ -1185,18 +1185,16 @@
 %0A  @Rule
-%0A 
  public 
@@ -1283,19 +1283,15 @@
 ject
-%0A 
  @Input
-%0A 
  pri
@@ -1539,29 +1539,16 @@
 o.js%22), 
-%0A            
 %22goog.mo
@@ -1561,28 +1561,16 @@
 foo');%22,
-%0A           
  %22export
@@ -1824,33 +1824,24 @@
 til.compile(
-%0A        
 createSource
@@ -1841,37 +1841,24 @@
 eSourceFile(
-%0A            
 inputFs.getP
@@ -1874,36 +1874,24 @@
 le/foo.js%22),
-%0A           
  %22export cla
@@ -2188,33 +2188,24 @@
 til.compile(
-%0A        
 createSource
@@ -2205,37 +2205,24 @@
 eSourceFile(
-%0A            
 inputFs.getP
@@ -2242,28 +2242,16 @@
 oo.js%22),
-%0A           
  %22export

Format code