github.com/raychaser/docker@v1.5.0/engine/table_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  )
     8  
     9  func TestTableWriteTo(t *testing.T) {
    10  	table := NewTable("", 0)
    11  	e := &Env{}
    12  	e.Set("foo", "bar")
    13  	table.Add(e)
    14  	var buf bytes.Buffer
    15  	if _, err := table.WriteTo(&buf); err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	output := make(map[string]string)
    19  	if err := json.Unmarshal(buf.Bytes(), &output); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	if len(output) != 1 {
    23  		t.Fatalf("Incorrect output: %v", output)
    24  	}
    25  	if val, exists := output["foo"]; !exists || val != "bar" {
    26  		t.Fatalf("Inccorect output: %v", output)
    27  	}
    28  }
    29  
    30  func TestTableSortStringValue(t *testing.T) {
    31  	table := NewTable("Key", 0)
    32  
    33  	e := &Env{}
    34  	e.Set("Key", "A")
    35  	table.Add(e)
    36  
    37  	e = &Env{}
    38  	e.Set("Key", "D")
    39  	table.Add(e)
    40  
    41  	e = &Env{}
    42  	e.Set("Key", "B")
    43  	table.Add(e)
    44  
    45  	e = &Env{}
    46  	e.Set("Key", "C")
    47  	table.Add(e)
    48  
    49  	table.Sort()
    50  
    51  	if len := table.Len(); len != 4 {
    52  		t.Fatalf("Expected 4, got %d", len)
    53  	}
    54  
    55  	if value := table.Data[0].Get("Key"); value != "A" {
    56  		t.Fatalf("Expected A, got %s", value)
    57  	}
    58  
    59  	if value := table.Data[1].Get("Key"); value != "B" {
    60  		t.Fatalf("Expected B, got %s", value)
    61  	}
    62  
    63  	if value := table.Data[2].Get("Key"); value != "C" {
    64  		t.Fatalf("Expected C, got %s", value)
    65  	}
    66  
    67  	if value := table.Data[3].Get("Key"); value != "D" {
    68  		t.Fatalf("Expected D, got %s", value)
    69  	}
    70  }
    71  
    72  func TestTableReverseSortStringValue(t *testing.T) {
    73  	table := NewTable("Key", 0)
    74  
    75  	e := &Env{}
    76  	e.Set("Key", "A")
    77  	table.Add(e)
    78  
    79  	e = &Env{}
    80  	e.Set("Key", "D")
    81  	table.Add(e)
    82  
    83  	e = &Env{}
    84  	e.Set("Key", "B")
    85  	table.Add(e)
    86  
    87  	e = &Env{}
    88  	e.Set("Key", "C")
    89  	table.Add(e)
    90  
    91  	table.ReverseSort()
    92  
    93  	if len := table.Len(); len != 4 {
    94  		t.Fatalf("Expected 4, got %d", len)
    95  	}
    96  
    97  	if value := table.Data[0].Get("Key"); value != "D" {
    98  		t.Fatalf("Expected D, got %s", value)
    99  	}
   100  
   101  	if value := table.Data[1].Get("Key"); value != "C" {
   102  		t.Fatalf("Expected B, got %s", value)
   103  	}
   104  
   105  	if value := table.Data[2].Get("Key"); value != "B" {
   106  		t.Fatalf("Expected C, got %s", value)
   107  	}
   108  
   109  	if value := table.Data[3].Get("Key"); value != "A" {
   110  		t.Fatalf("Expected A, got %s", value)
   111  	}
   112  }