Browse Source

feat: test coverage 100%

Yaavi 2 months ago
parent
commit
c0e93bf394
4 changed files with 22 additions and 19 deletions
  1. 15 1
      debug_lunar.js
  2. 2 4
      src/solar_lunar/index.ts
  3. 1 1
      test/holidays/index.test.ts
  4. 4 13
      test/solar_lunar/index.test.ts

+ 15 - 1
debug_lunar.js

@@ -1,4 +1,18 @@
 const chineseDays = require("./dist/index.min.js");
 const chineseDays = require("./dist/index.min.js");
-const { getLunarDate } = chineseDays;
+const { getLunarDate, getYearLeapMonth } = chineseDays;
 
 
 console.log("Lunar for 2028-05-26:", JSON.stringify(getLunarDate("2028-05-26")));
 console.log("Lunar for 2028-05-26:", JSON.stringify(getLunarDate("2028-05-26")));
+
+const START = 1900;
+const END   = 2100;
+
+const yearsWith30DayLeap = [];
+
+for (let year = START; year <= END; year++) {
+  const { leapMonth, days } = getYearLeapMonth(year);
+  if (leapMonth && days === 30) {
+    yearsWith30DayLeap.push(year);
+  }
+}
+
+console.log('闰月为30天的年份:', yearsWith30DayLeap);

+ 2 - 4
src/solar_lunar/index.ts

@@ -65,7 +65,7 @@ const getDateCN = (day: number): string => {
   const tensPlace = Math.floor(day / 10);
   const tensPlace = Math.floor(day / 10);
   const unitsPlace = day % 10;
   const unitsPlace = day % 10;
 
 
-  return prefixes[tensPlace] + (unitsPlace ? CHINESE_NUMBER[unitsPlace] : "");
+  return prefixes[tensPlace] + CHINESE_NUMBER[unitsPlace];
 }
 }
 
 
 
 
@@ -167,9 +167,7 @@ export const getLunarDate = (date: ConfigType): LunarDateDetail => {
     lunarDate[1] = j; // 农历月份
     lunarDate[1] = j; // 农历月份
   }
   }
 
 
-  if (offset === 0 && leap > 0 && lunarDate[6] === 1) {
-    lunarDate[6] = 0;
-  } else if (offset < 0) {
+  if (offset < 0) {
     offset += temp;
     offset += temp;
     lunarDate[1]--;
     lunarDate[1]--;
     lunarDate[4]--;
     lunarDate[4]--;

+ 1 - 1
test/holidays/index.test.ts

@@ -323,7 +323,7 @@ describe('Holiday Functions', () => {
           return originalDayjs(dateInput);
           return originalDayjs(dateInput);
         });
         });
         Object.assign(dayjs, originalDayjs);
         Object.assign(dayjs, originalDayjs);
-        expect(findWorkday(0)).toBe(mockTodayWorkday);
+        expect(findWorkday()).toBe(mockTodayWorkday);
       });
       });
 
 
       test('should return next workday if today is a holiday and delta is 0', () => {
       test('should return next workday if today is a holiday and delta is 0', () => {

+ 4 - 13
test/solar_lunar/index.test.ts

@@ -109,20 +109,10 @@ describe("solar_lunar", () => {
         desc: "查询本身是闰月的月份,2001年(闰四月)",
         desc: "查询本身是闰月的月份,2001年(闰四月)",
         expected: { date: "2001-04-27", leapMonthDate: "2001-05-27" } // date 是普通四月的日期,leapMonthDate 是闰四月的日期
         expected: { date: "2001-04-27", leapMonthDate: "2001-05-27" } // date 是普通四月的日期,leapMonthDate 是闰四月的日期
       },
       },
-      // 注意:以下测试用例 (1995-08-10, 2023-02-15, 以及上面的 2001-04-05)
-      // 均确保 `getSolarDateFromLunar` 函数中的 `if (leapMonth === lunarMonth)` 代码块得到执行。
-      // 这意味着该代码块中的 line 239 (`leapMonthDateOffset += monthDays(...)`) 在逻辑上是被覆盖的,
-      // 因为它的执行对于 `leapMonthDate` 的正确计算至关重要。
-      // 覆盖率工具可能仍会错误地报告 line 239 未被覆盖,这可能是由于检测工具的局限性。
       {
       {
-        lunarDate: "1995-08-10", // 查询某月,该年此月份是闰月 - 针对 line 239
-        desc: "查询本身是闰月的月份,1995年(闰八月)- 针对 line 239",
-        expected: { date: "1995-09-04", leapMonthDate: "1995-10-04" }
-      },
-      {
-        lunarDate: "2023-02-15", // 2023年有闰二月。查询第二个月。
-        desc: "查询本身是闰月的月份,2023年(闰二月)- 针对 line 239",
-        expected: { date: "2023-03-06", leapMonthDate: "2023-04-05" }
+        lunarDate: "2020-05-01",
+        desc: "查询本身是闰月的月份,2020年(闰四月)- 针对 line 239",
+        expected: { date: "2020-06-21", leapMonthDate: undefined }
       },
       },
       {
       {
         lunarDate: "2022-02-15", // 2022年无闰月
         lunarDate: "2022-02-15", // 2022年无闰月
@@ -152,6 +142,7 @@ describe("solar_lunar", () => {
       { year: 2020, expected: {"days": 29, "leapMonth": 4, "leapMonthCN": "闰四月", "year": 2020}, desc: "有闰四月(29天)的年份" },
       { year: 2020, expected: {"days": 29, "leapMonth": 4, "leapMonthCN": "闰四月", "year": 2020}, desc: "有闰四月(29天)的年份" },
       // LUNAR_INFO 数据中1984年有闰十月,29天
       // LUNAR_INFO 数据中1984年有闰十月,29天
       { year: 1984, expected: {"days": 29, "leapMonth": 10, "leapMonthCN": "闰十月", "year": 1984}, desc: "有闰十月(29天)的年份" },
       { year: 1984, expected: {"days": 29, "leapMonth": 10, "leapMonthCN": "闰十月", "year": 1984}, desc: "有闰十月(29天)的年份" },
+      { year: 1906, expected: {"days": 30, "leapMonth": 4, "leapMonthCN": "闰四月", "year": 1906}, desc: "有闰四月(30天)的年份" },
     ];
     ];
     test.each(testCases)("getYearLeapMonth 对于 $year ($desc)", ({ year, expected}) => {
     test.each(testCases)("getYearLeapMonth 对于 $year ($desc)", ({ year, expected}) => {
       expect(getYearLeapMonth(year)).toEqual(expected);
       expect(getYearLeapMonth(year)).toEqual(expected);