Browse Source

cleanup: remove unused function, add test coverage for cron.Entry

Rob Figueiredo 6 years ago
parent
commit
86fcb220ef
2 changed files with 22 additions and 34 deletions
  1. 18 2
      cron_test.go
  2. 4 32
      parser.go

+ 18 - 2
cron_test.go

@@ -424,10 +424,18 @@ func TestJob(t *testing.T) {
 	cron := newWithSeconds()
 	cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"})
 	cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"})
-	cron.AddJob("* * * * * ?", testJob{wg, "job2"})
+	job2, _ := cron.AddJob("* * * * * ?", testJob{wg, "job2"})
 	cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"})
 	cron.Schedule(Every(5*time.Second+5*time.Nanosecond), testJob{wg, "job4"})
-	cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"})
+	job5 := cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"})
+
+	// Test getting an Entry pre-Start.
+	if actualName := cron.Entry(job2).Job.(testJob).name; actualName != "job2" {
+		t.Error("wrong job retrieved:", actualName)
+	}
+	if actualName := cron.Entry(job5).Job.(testJob).name; actualName != "job5" {
+		t.Error("wrong job retrieved:", actualName)
+	}
 
 	cron.Start()
 	defer cron.Stop()
@@ -451,6 +459,14 @@ func TestJob(t *testing.T) {
 			t.Fatalf("Jobs not in the right order.  (expected) %s != %s (actual)", expecteds, actuals)
 		}
 	}
+
+	// Test getting Entries.
+	if actualName := cron.Entry(job2).Job.(testJob).name; actualName != "job2" {
+		t.Error("wrong job retrieved:", actualName)
+	}
+	if actualName := cron.Entry(job5).Job.(testJob).name; actualName != "job5" {
+		t.Error("wrong job retrieved:", actualName)
+	}
 }
 
 type ZeroSchedule struct{}

+ 4 - 32
parser.go

@@ -214,42 +214,14 @@ func normalizeFields(fields []string, options ParseOption) ([]string, error) {
 	return expandedFields, nil
 }
 
-// expandOptionalFields returns fields with any optional fields added in at
-// their default value, if not provided.
-//
-// It panics if the input does not fulfill the following precondition:
-//   1. (# options fields) - (1 optional field) <= len(fields) <= (# options fields)
-//   2. Any optional fields have had their field added.
-//      For example, options&SecondOptional implies options&Second)
-func expandOptionalFields(fields []string, options ParseOption) []string {
-	expectedFields := 0
-	for _, place := range places {
-		if options&place > 0 {
-			expectedFields++
-		}
-	}
-	switch {
-	case len(fields) == expectedFields:
-		return fields
-	case len(fields) == expectedFields-1:
-		switch {
-		case options&DowOptional > 0:
-			return append(fields, defaults[5]) // TODO: improve access to default
-		case options&SecondOptional > 0:
-			return append([]string{defaults[0]}, fields...)
-		}
-	}
-	panic(fmt.Errorf("expected %d fields, got %d", expectedFields, len(fields)))
-}
-
 var standardParser = NewParser(
 	Minute | Hour | Dom | Month | Dow | Descriptor,
 )
 
-// ParseStandard returns a new crontab schedule representing the given standardSpec
-// (https://en.wikipedia.org/wiki/Cron). It differs from Parse requiring to always
-// pass 5 entries representing: minute, hour, day of month, month and day of week,
-// in that order. It returns a descriptive error if the spec is not valid.
+// ParseStandard returns a new crontab schedule representing the given
+// standardSpec (https://en.wikipedia.org/wiki/Cron). It requires 5 entries
+// representing: minute, hour, day of month, month and day of week, in that
+// order. It returns a descriptive error if the spec is not valid.
 //
 // It accepts
 //   - Standard crontab specs, e.g. "* * * * ?"