|
| 1 | +// Additional tests for new_branch.rs to increase coverage |
| 2 | + |
| 3 | +use git_x::new_branch::*; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_get_validation_rules() { |
| 7 | + let rules = get_validation_rules(); |
| 8 | + assert!(!rules.is_empty()); |
| 9 | + |
| 10 | + // Check that rules contain expected validation information |
| 11 | + let rules_text = rules.join(" "); |
| 12 | + assert!( |
| 13 | + rules_text.contains("empty") |
| 14 | + || rules_text.contains("dash") |
| 15 | + || rules_text.contains("spaces") |
| 16 | + || rules_text.contains("contain") |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn test_format_error_message() { |
| 22 | + assert_eq!(format_error_message("test error"), "❌ test error"); |
| 23 | + assert_eq!(format_error_message(""), "❌ "); |
| 24 | + assert_eq!( |
| 25 | + format_error_message("Branch creation failed"), |
| 26 | + "❌ Branch creation failed" |
| 27 | + ); |
| 28 | + assert_eq!( |
| 29 | + format_error_message("Invalid branch name"), |
| 30 | + "❌ Invalid branch name" |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn test_format_branch_exists_message() { |
| 36 | + assert_eq!( |
| 37 | + format_branch_exists_message("main"), |
| 38 | + "❌ Branch 'main' already exists" |
| 39 | + ); |
| 40 | + assert_eq!( |
| 41 | + format_branch_exists_message("feature/test"), |
| 42 | + "❌ Branch 'feature/test' already exists" |
| 43 | + ); |
| 44 | + assert_eq!( |
| 45 | + format_branch_exists_message(""), |
| 46 | + "❌ Branch '' already exists" |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn test_format_invalid_base_message() { |
| 52 | + assert_eq!( |
| 53 | + format_invalid_base_message("nonexistent"), |
| 54 | + "❌ Base branch or ref 'nonexistent' does not exist" |
| 55 | + ); |
| 56 | + assert_eq!( |
| 57 | + format_invalid_base_message("origin/missing"), |
| 58 | + "❌ Base branch or ref 'origin/missing' does not exist" |
| 59 | + ); |
| 60 | + assert_eq!( |
| 61 | + format_invalid_base_message(""), |
| 62 | + "❌ Base branch or ref '' does not exist" |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn test_format_creating_branch_message() { |
| 68 | + assert_eq!( |
| 69 | + format_creating_branch_message("new-branch", "main"), |
| 70 | + "🌿 Creating branch 'new-branch' from 'main'..." |
| 71 | + ); |
| 72 | + assert_eq!( |
| 73 | + format_creating_branch_message("feature/awesome", "develop"), |
| 74 | + "🌿 Creating branch 'feature/awesome' from 'develop'..." |
| 75 | + ); |
| 76 | + assert_eq!( |
| 77 | + format_creating_branch_message("", ""), |
| 78 | + "🌿 Creating branch '' from ''..." |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn test_format_success_message() { |
| 84 | + assert_eq!( |
| 85 | + format_success_message("new-branch"), |
| 86 | + "✅ Created and switched to branch 'new-branch'" |
| 87 | + ); |
| 88 | + assert_eq!( |
| 89 | + format_success_message("feature/test"), |
| 90 | + "✅ Created and switched to branch 'feature/test'" |
| 91 | + ); |
| 92 | + assert_eq!( |
| 93 | + format_success_message(""), |
| 94 | + "✅ Created and switched to branch ''" |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn test_get_git_branch_args() { |
| 100 | + let args = get_git_branch_args(); |
| 101 | + assert_eq!(args.len(), 2); |
| 102 | + assert_eq!(args[0], "branch"); |
| 103 | + assert_eq!(args[1], "-"); |
| 104 | +} |
| 105 | + |
| 106 | +#[test] |
| 107 | +fn test_get_git_switch_args() { |
| 108 | + let args = get_git_switch_args(); |
| 109 | + assert_eq!(args.len(), 2); |
| 110 | + assert_eq!(args[0], "switch"); |
| 111 | + assert_eq!(args[1], "-"); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn test_message_formatting_edge_cases() { |
| 116 | + // Test with special characters and edge cases |
| 117 | + assert!( |
| 118 | + format_creating_branch_message("test/branch-123", "origin/main") |
| 119 | + .contains("test/branch-123") |
| 120 | + ); |
| 121 | + assert!( |
| 122 | + format_creating_branch_message("test/branch-123", "origin/main").contains("origin/main") |
| 123 | + ); |
| 124 | + |
| 125 | + assert!(format_success_message("feature/issue-456").contains("feature/issue-456")); |
| 126 | + assert!(format_branch_exists_message("hotfix/urgent").contains("hotfix/urgent")); |
| 127 | + assert!( |
| 128 | + format_invalid_base_message("refs/remotes/origin/feature") |
| 129 | + .contains("refs/remotes/origin/feature") |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn test_format_consistency() { |
| 135 | + // Test that all format functions return non-empty strings for reasonable inputs |
| 136 | + assert!(!format_error_message("test").is_empty()); |
| 137 | + assert!(!format_branch_exists_message("test").is_empty()); |
| 138 | + assert!(!format_invalid_base_message("test").is_empty()); |
| 139 | + assert!(!format_creating_branch_message("test", "main").is_empty()); |
| 140 | + assert!(!format_success_message("test").is_empty()); |
| 141 | + |
| 142 | + // Test that they include expected emojis or symbols |
| 143 | + assert!(format_error_message("test").contains("❌")); |
| 144 | + assert!(format_branch_exists_message("test").contains("❌")); |
| 145 | + assert!(format_invalid_base_message("test").contains("❌")); |
| 146 | + assert!(format_creating_branch_message("test", "main").contains("🌿")); |
| 147 | + assert!(format_success_message("test").contains("✅")); |
| 148 | +} |
0 commit comments