github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/extent_test.go (about)

     1  package chrono_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/go-chrono/chrono"
     8  )
     9  
    10  func TestExtent_Truncate(t *testing.T) {
    11  	t.Run("positive", func(t *testing.T) {
    12  		e := 1*chrono.Hour + 20*chrono.Minute + 5*chrono.Second + 42*chrono.Millisecond + 307*chrono.Microsecond
    13  
    14  		if e2 := e.Truncate(200 * chrono.Microsecond); e2 != 4805042200000 {
    15  			t.Errorf("e.Truncate() = %d, want 4805042200000", e2)
    16  		}
    17  	})
    18  
    19  	t.Run("negative", func(t *testing.T) {
    20  		e := -1*chrono.Hour - 20*chrono.Minute - 5*chrono.Second - 42*chrono.Millisecond - 307*chrono.Microsecond
    21  
    22  		if e2 := e.Truncate(200 * chrono.Microsecond); e2 != -4805042200000 {
    23  			t.Errorf("e.Truncate() = %d, want -4805042200000", e2)
    24  		}
    25  	})
    26  }
    27  
    28  func TestExtent_Units(t *testing.T) {
    29  	e := (12 * chrono.Hour) + (34 * chrono.Minute) + (56 * chrono.Second) + (7 * chrono.Nanosecond)
    30  
    31  	hours, mins, secs, nsec := e.Units()
    32  	if hours != 12 {
    33  		t.Errorf("expecting 12 hours, got %d", hours)
    34  	}
    35  
    36  	if mins != 34 {
    37  		t.Errorf("expecting 34 mins, got %d", mins)
    38  	}
    39  
    40  	if secs != 56 {
    41  		t.Errorf("expecting 56 secs, got %d", secs)
    42  	}
    43  
    44  	if nsec != 7 {
    45  		t.Errorf("expecting 7 nsecs, got %d", nsec)
    46  	}
    47  }
    48  
    49  func TestExtent_Format(t *testing.T) {
    50  	for _, tt := range formatDurationCases {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			t.Run("positive", func(t *testing.T) {
    53  				if out := tt.of.Format(tt.exclusive...); out != tt.expected {
    54  					t.Errorf("formatted extent = %s, want %s", out, tt.expected)
    55  				}
    56  			})
    57  
    58  			t.Run("negative", func(t *testing.T) {
    59  				expected := tt.expected
    60  				if tt.of != 0 {
    61  					expected = "-" + expected
    62  				}
    63  
    64  				if out := (tt.of * -1).Format(tt.exclusive...); out != expected {
    65  					t.Errorf("formatted extent = %s, want %s", out, expected)
    66  				}
    67  			})
    68  		})
    69  	}
    70  }
    71  
    72  func TestExtent_Parse(t *testing.T) {
    73  	for _, tt := range parseDurationCases {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			for _, sign := range []string{"", "+", "-"} {
    76  				t.Run(sign, func(t *testing.T) {
    77  					input := sign + tt.input
    78  					expected := tt.expected
    79  					if sign == "-" {
    80  						expected *= -1
    81  					}
    82  
    83  					run := func() {
    84  						var e chrono.Extent
    85  						if err := e.Parse(input); err != nil {
    86  							t.Errorf("failed to parse duation: %v", err)
    87  						} else if e != expected {
    88  							t.Errorf("parsed extent = %v, want %v", e, expected)
    89  						}
    90  					}
    91  
    92  					t.Run("dots", func(t *testing.T) {
    93  						run()
    94  					})
    95  
    96  					t.Run("commas", func(t *testing.T) {
    97  						tt.input = strings.ReplaceAll(tt.input, ".", ",")
    98  						run()
    99  					})
   100  				})
   101  			}
   102  		})
   103  	}
   104  
   105  	t.Run("overflows", func(t *testing.T) {
   106  		var e chrono.Extent
   107  		if err := e.Parse("PT9223372037S"); err == nil {
   108  			t.Error("expecting error but got nil")
   109  		}
   110  	})
   111  
   112  	t.Run("underflows", func(t *testing.T) {
   113  		var d chrono.Duration
   114  		if err := d.Parse("PT-9223372035S"); err == nil {
   115  			t.Error("expecting error but got nil")
   116  		}
   117  	})
   118  }