vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

vfmt: v fmt breaks code #7352

Closed zakuro9715 closed 3 years ago

zakuro9715 commented 3 years ago

V version: OS:

What did you do?

cat vlib/time/parse_test.v

import time

fn test_parse() {
    s := '2018-01-27 12:48:34'
    t := time.parse(s) or {
        assert false
        return
    }
    assert t.year == 2018 &&
        t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
    assert t.unix == 1517057314
}

fn test_parse_invalid() {
    s := 'Invalid time string'
    time.parse(s) or {
        assert true
        return
    }
    assert false
}

fn test_parse_rfc2822() {
    s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
    t1 := time.parse_rfc2822(s1) or {
        assert false
        return
    }
    assert t1.year == 2019 &&
        t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
    assert t1.unix == 1576130865
    s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
    t2 := time.parse_rfc2822(s2) or {
        assert false
        return
    }
    assert t2.year == 2019 &&
        t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
    assert t2.unix == 1576130865
}

fn test_parse_rfc2822_invalid() {
    s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
    time.parse_rfc2822(s3) or {
        assert true
        return
    }
    assert false
}

fn test_parse_iso8601() {
    // Because the offset between local time and UTC is not constant in regions
    // that use daylight saving times we need to calculate separete offsets for
    // summer and winter
    offset_summer := time.Duration(time.new_time(year: 2020, month: 6, day: 5, hour: 15).to_local_time() -
        time.new_time(year: 2020, month: 6, day: 5, hour: 15))
    offset_winter := time.Duration(time.new_time(year: 2020, month: 11, day: 5, hour: 15).to_local_time() -
        time.new_time(year: 2020, month: 11, day: 5, hour: 15))
    formats := [
        '2020-06-05T15:38:06Z',
        '2020-06-05T15:38:06.015959Z',
        '2020-06-05T15:38:06.015959+00:00',
        '2020-06-05T15:38:06.015959+02:00',
        '2020-06-05T15:38:06.015959-02:00',
        '2020-11-05T15:38:06.015959Z'
    ]
    times := [
        [2020, 6, 5, 15, 38, 6, 0],
        [2020, 6, 5, 15, 38, 6, 15959],
        [2020, 6, 5, 15, 38, 6, 15959],
        [2020, 6, 5, 13, 38, 6, 15959],
        [2020, 6, 5, 17, 38, 6, 15959],
        [2020, 11, 5, 15, 38, 6, 15959],
    ]
    for i, format in formats {
        t := time.parse_iso8601(format) or {
            assert false
            continue
        }
        data := times[i]
        t2 := time.new_time(
            year: data[0]
            month: data[1]
            day: data[2]
            hour: data[3]
            minute: data[4]
            second: data[5]
            microsecond: data[6]
        ).add(if i <= 4 { offset_summer } else { offset_winter })
        assert t.year == t2.year
        assert t.month == t2.month
        assert t.day == t2.day
        assert t.hour == t2.hour
        assert t.minute == t2.minute
        assert t.second == t2.second
        assert t.microsecond == t2.microsecond
    }
}

fn test_parse_iso8601_local() {
    format := '2020-06-05T15:38:06.015959'
    t := time.parse_iso8601(format) or {
        assert false
        return
    }
    assert t.year == 2020
    assert t.month == 6
    assert t.day == 5
    assert t.hour == 15
    assert t.minute == 38
    assert t.second == 6
    assert t.microsecond == 15959
}

fn test_parse_iso8601_invalid() {
    formats := [
        '',
        '2020-06-05X15:38:06.015959Z',
        '2020-06-05T15:38:06.015959X',
        '2020-06-05T15:38:06.015959+0000',
        '2020-06-05T',
        '2020-06-05Z',
        '2020-06-05+00:00',
        '15:38:06',
    ]
    for format in formats {
        time.parse_iso8601(format) or {
            assert true
            continue
        }
        assert false
    }
}

v fmt -w vlib/time/parse_test.v

What did you expect to see?

Don't break code

What did you see instead?

git diff

diff --git a/vlib/time/parse_test.v b/vlib/time/parse_test.v
index 9cb580f6..8a5c065d 100644
--- a/vlib/time/parse_test.v
+++ b/vlib/time/parse_test.v
@@ -52,17 +52,205 @@ fn test_parse_iso8601() {
    // Because the offset between local time and UTC is not constant in regions
    // that use daylight saving times we need to calculate separete offsets for
    // summer and winter
-   offset_summer := time.Duration(time.new_time(year: 2020, month: 6, day: 5, hour: 15).to_local_time() -
-       time.new_time(year: 2020, month: 6, day: 5, hour: 15))
-   offset_winter := time.Duration(time.new_time(year: 2020, month: 11, day: 5, hour: 15).to_local_time() -
-       time.new_time(year: 2020, month: 11, day: 5, hour: 15))
+   offset_summer := time.Duration(fn test_parse() {
+   s := '2018-01-27 12:48:34'
+   t := time.parse(s) or {
+       assert false
+       return
+   }
+   assert t.year == 2018 &&
+       t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
+   assert t.unix == 1517057314
+}
+
+fn test_parse_invalid() {
+   s := 'Invalid time string'
+   time.parse(s) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_rfc2822() {
+   s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
+   t1 := time.parse_rfc2822(s1) or {
+       assert false
+       return
+   }
+   assert t1.year == 2019 &&
+       t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
+   assert t1.unix == 1576130865
+   s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
+   t2 := time.parse_rfc2822(s2) or {
+       assert false
+       return
+   }
+   assert t2.year == 2019 &&
+       t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
+   assert t2.unix == 1576130865
+}
+
+fn test_parse_rfc2822_invalid() {
+   s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
+   time.parse_rfc2822(s3) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_iso8601() {
+   // Because the offset between local time and UTC is not constant in regions
+   // that use daylight saving times we need to calculate separete offsets for
+   // summer and winter
+   offset_summer := time.Duration(time.new_time(
+       year: 2020
+       month: 6
+       day: 5
+       hour: 15
+   ).to_local_time() -
+           time.new_time(
+       year: 2020
+       month: 6
+       day: 5
+       hour: 15
+   ))
+   offset_winter := time.Duration(fn test_parse() {
+   s := '2018-01-27 12:48:34'
+   t := time.parse(s) or {
+       assert false
+       return
+   }
+   assert t.year == 2018 &&
+       t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
+   assert t.unix == 1517057314
+}
+
+fn test_parse_invalid() {
+   s := 'Invalid time string'
+   time.parse(s) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_rfc2822() {
+   s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
+   t1 := time.parse_rfc2822(s1) or {
+       assert false
+       return
+   }
+   assert t1.year == 2019 &&
+       t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
+   assert t1.unix == 1576130865
+   s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
+   t2 := time.parse_rfc2822(s2) or {
+       assert false
+       return
+   }
+   assert t2.year == 2019 &&
+       t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
+   assert t2.unix == 1576130865
+}
+
+fn test_parse_rfc2822_invalid() {
+   s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
+   time.parse_rfc2822(s3) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_iso8601() {
+   // Because the offset between local time and UTC is not constant in regions
+   // that use daylight saving times we need to calculate separete offsets for
+   // summer and winter
+   offset_summer := time.Duration(fn test_parse() {
+   s := '2018-01-27 12:48:34'
+   t := time.parse(s) or {
+       assert false
+       return
+   }
+   assert t.year == 2018 &&
+       t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
+   assert t.unix == 1517057314
+}
+
+fn test_parse_invalid() {
+   s := 'Invalid time string'
+   time.parse(s) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_rfc2822() {
+   s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
+   t1 := time.parse_rfc2822(s1) or {
+       assert false
+       return
+   }
+   assert t1.year == 2019 &&
+       t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
+   assert t1.unix == 1576130865
+   s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
+   t2 := time.parse_rfc2822(s2) or {
+       assert false
+       return
+   }
+   assert t2.year == 2019 &&
+       t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
+   assert t2.unix == 1576130865
+}
+
+fn test_parse_rfc2822_invalid() {
+   s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
+   time.parse_rfc2822(s3) or {
+       assert true
+       return
+   }
+   assert false
+}
+
+fn test_parse_iso8601() {
+   // Because the offset between local time and UTC is not constant in regions
+   // that use daylight saving times we need to calculate separete offsets for
+   // summer and winter
+   offset_summer := time.Duration(time.new_time(
+       year: 2020
+       month: 6
+       day: 5
+       hour: 15
+   ).to_local_time() -
+           time.new_time(
+       year: 2020
+       month: 6
+       day: 5
+       hour: 15
+   ))
+   offset_winter := time.Duration(time.new_time(
+       year: 2020
+       month: 11
+       day: 5
+       hour: 15
+   ).to_local_time() -
+           time.new_time(
+       year: 2020
+       month: 11
+       day: 5
+       hour: 15
+   ))
    formats := [
        '2020-06-05T15:38:06Z',
        '2020-06-05T15:38:06.015959Z',
        '2020-06-05T15:38:06.015959+00:00',
        '2020-06-05T15:38:06.015959+02:00',
        '2020-06-05T15:38:06.015959-02:00',
-       '2020-11-05T15:38:06.015959Z'
+       '2020-11-05T15:38:06.015959Z',
    ]
    times := [
        [2020, 6, 5, 15, 38, 6, 0],
spytheman commented 3 years ago

I can not reproduce that with latest V 0.1.30 7426544 . Please provide more details about your environment (the output of v doctor). For me:

0[10:25:02]delian@nemesis: /v/pv $ ./v fmt -diff vlib/time/parse_test.v 
--- /v/pv/vlib/time/parse_test.v        2020-12-16 10:22:00.927875096 +0200
+++ /tmp/vfmt_01ESNAE6BGDAWVQFX1ENRTS817_parse_test.v   2020-12-16 10:25:04.623408937 +0200
@@ -63,5 +63,5 @@ fn test_parse_iso8601() {
                '2020-06-05T15:38:06.015959+02:00',
                '2020-06-05T15:38:06.015959-02:00',
-               '2020-11-05T15:38:06.015959Z'
+               '2020-11-05T15:38:06.015959Z',
        ]
        times := [
0[10:25:04]delian@nemesis: /v/pv $ ./v fmt -w vlib/time/parse_test.v 
Reformatted file: /v/pv/vlib/time/parse_test.v
0[10:25:09]delian@nemesis: /v/pv $ git diff 
diff --git a/vlib/time/parse_test.v b/vlib/time/parse_test.v
index 9cb580f6d..04fa612b3 100644
--- a/vlib/time/parse_test.v
+++ b/vlib/time/parse_test.v
@@ -62,7 +62,7 @@ fn test_parse_iso8601() {
                '2020-06-05T15:38:06.015959+00:00',
                '2020-06-05T15:38:06.015959+02:00',
                '2020-06-05T15:38:06.015959-02:00',
-               '2020-11-05T15:38:06.015959Z'
+               '2020-11-05T15:38:06.015959Z',
        ]
        times := [
                [2020, 6, 5, 15, 38, 6, 0],
0[10:25:12]delian@nemesis: /v/pv $ ./v -stats vlib/time/parse_test.v
checker summary: 0 V errors, 0 V warnings
compilation took: 221 ms
running tests in: vlib/time/parse_test.v
     OK   [1/7]     0.055 ms     2 asserts | main.test_parse()
     OK   [2/7]     0.004 ms     1 assert  | main.test_parse_invalid()
     OK   [3/7]     0.032 ms     4 asserts | main.test_parse_rfc2822()
     OK   [4/7]     0.009 ms     1 assert  | main.test_parse_rfc2822_invalid()
     OK   [5/7]     0.054 ms    42 asserts | main.test_parse_iso8601()
     OK   [6/7]     0.008 ms     7 asserts | main.test_parse_iso8601_local()
     OK   [7/7]     0.009 ms     8 asserts | main.test_parse_iso8601_invalid()
                    0.317 ms <=== total time spent running V tests in "parse_test.v"
                 ok, fail, skip, total =    65,     0,     0,    65
0[10:25:24]delian@nemesis: /v/pv $
spytheman commented 3 years ago

The whole of time and os is now checked with v test-cleancode, so it should be invariant under v fmt -w runs.

zakuro9715 commented 3 years ago

Sorry. I forgot re compile head. After v self, it works