github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/otto/core_test.go (about)

     1  package otto
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/otto/app"
     9  )
    10  
    11  func TestCoreApp(t *testing.T) {
    12  	// Make a core that returns a fixed app
    13  	coreConfig := TestCoreConfig(t)
    14  	coreConfig.Appfile = TestAppfile(t, testPath("basic", "Appfile"))
    15  	appMock := TestApp(t, TestAppTuple, coreConfig)
    16  	core := testCore(t, coreConfig)
    17  
    18  	// Get the App
    19  	app, _, err := core.App()
    20  	if err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  
    24  	if app != appMock {
    25  		t.Fatal("should be equal")
    26  	}
    27  }
    28  
    29  func TestCoreCompile_close(t *testing.T) {
    30  	// Make a core that returns a fixed app
    31  	coreConfig := TestCoreConfig(t)
    32  	coreConfig.Appfile = TestAppfile(t, testPath("basic", "Appfile"))
    33  	appMock := TestApp(t, TestAppTuple, coreConfig)
    34  	core := testCore(t, coreConfig)
    35  
    36  	// Compile!
    37  	if err := core.Compile(); err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  
    41  	if !appMock.CompileCalled {
    42  		t.Fatal("compile should be called")
    43  	}
    44  	if !appMock.CloseCalled {
    45  		t.Fatal("close should be called")
    46  	}
    47  }
    48  
    49  func TestCoreCompile_customizationFilter(t *testing.T) {
    50  	// Make a core that returns a fixed app
    51  	coreConfig := TestCoreConfig(t)
    52  	coreConfig.Appfile = TestAppfile(t, testPath("customization-app-filter", "Appfile"))
    53  	appMock := TestApp(t, TestAppTuple, coreConfig)
    54  	core := testCore(t, coreConfig)
    55  
    56  	// Compile!
    57  	if err := core.Compile(); err != nil {
    58  		t.Fatalf("err: %s", err)
    59  	}
    60  
    61  	if !appMock.CompileCalled {
    62  		t.Fatal("compile should be called")
    63  	}
    64  
    65  	// Verify our customizations
    66  	var keys []string
    67  	for _, c := range appMock.CompileContext.Appfile.Customization.Raw {
    68  		for k, _ := range c.Config {
    69  			keys = append(keys, k)
    70  		}
    71  	}
    72  	sort.Strings(keys)
    73  
    74  	expected := []string{"bar", "foo"}
    75  	if !reflect.DeepEqual(keys, expected) {
    76  		t.Fatalf("bad: %#v", keys)
    77  	}
    78  }
    79  
    80  func TestCoreDev_compileMetadata(t *testing.T) {
    81  	// Make a core that returns a fixed app
    82  	coreConfig := TestCoreConfig(t)
    83  	coreConfig.Appfile = TestAppfile(t, testPath("basic", "Appfile"))
    84  	appMock := TestApp(t, TestAppTuple, coreConfig)
    85  	core := testCore(t, coreConfig)
    86  
    87  	// Configure the app to return a specific version in the metadata
    88  	appMock.CompileResult = &app.CompileResult{Version: 12}
    89  
    90  	// Compile!
    91  	if err := core.Compile(); err != nil {
    92  		t.Fatalf("err: %s", err)
    93  	}
    94  
    95  	// Rebuild all the core so we have a fresh core
    96  	core = testCore(t, coreConfig)
    97  
    98  	// Run dev
    99  	if err := core.Dev(); err != nil {
   100  		t.Fatalf("err: %s", err)
   101  	}
   102  
   103  	// The context should have the right version
   104  	if appMock.DevContext.CompileResult.Version != 12 {
   105  		t.Fatalf("bad: %#v", appMock.DevContext.CompileResult)
   106  	}
   107  }
   108  
   109  func testCore(t *testing.T, config *CoreConfig) *Core {
   110  	core, err := NewCore(config)
   111  	if err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	return core
   116  }