gonum.org/v1/gonum@v0.14.0/unit/energy_test.go (about)

     1  // Code generated by "go generate gonum.org/v1/gonum/unit; DO NOT EDIT.
     2  
     3  // Copyright ©2019 The Gonum Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package unit
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  )
    13  
    14  func TestEnergy(t *testing.T) {
    15  	t.Parallel()
    16  	for _, value := range []float64{-1, 0, 1} {
    17  		var got Energy
    18  		err := got.From(Energy(value).Unit())
    19  		if err != nil {
    20  			t.Errorf("unexpected error for %T conversion: %v", got, err)
    21  		}
    22  		if got != Energy(value) {
    23  			t.Errorf("unexpected result from round trip of %T(%v): got: %v want: %v", got, value, got, value)
    24  		}
    25  		if got != got.Energy() {
    26  			t.Errorf("unexpected result from self interface method call: got: %#v want: %#v", got, value)
    27  		}
    28  		err = got.From(ether(1))
    29  		if err == nil {
    30  			t.Errorf("expected error for ether to %T conversion", got)
    31  		}
    32  	}
    33  }
    34  
    35  func TestEnergyFormat(t *testing.T) {
    36  	t.Parallel()
    37  	for _, test := range []struct {
    38  		value  Energy
    39  		format string
    40  		want   string
    41  	}{
    42  		{1.23456789, "%v", "1.23456789 J"},
    43  		{1.23456789, "%.1v", "1 J"},
    44  		{1.23456789, "%20.1v", "                 1 J"},
    45  		{1.23456789, "%20v", "        1.23456789 J"},
    46  		{1.23456789, "%1v", "1.23456789 J"},
    47  		{1.23456789, "%#v", "unit.Energy(1.23456789)"},
    48  		{1.23456789, "%s", "%!s(unit.Energy=1.23456789 J)"},
    49  	} {
    50  		got := fmt.Sprintf(test.format, test.value)
    51  		if got != test.want {
    52  			t.Errorf("Format %q %v: got: %q want: %q", test.format, test.value, got, test.want)
    53  		}
    54  	}
    55  }