Skip to content

Commit 44c52e6

Browse files
author
Ben Stern
committed
Fix Unicode minus handling
* also change output to use Unicode minuses instead of - for minuses * cargo update
1 parent e0800a3 commit 44c52e6

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/dice.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,10 @@ pub mod test {
475475
($text: literal, $expect: expr) => {
476476
let parsed = $text.parse::<Dice>().unwrap();
477477
let provided = $expect.unwrap();
478-
expect_dice_similar!(parsed, provided)
478+
assert!(parsed.similar(&provided));
479479
};
480480
($d1: expr, $d2: expr) => {
481-
//println!("Comparing:\n\t{:?}\n\t{:?}", $d1, $d2);
482-
assert_eq!($d1.count(), $d2.count());
483-
assert_eq!($d1.sides(), $d2.sides());
484-
assert_eq!($d1.fuse(), $d2.fuse());
485-
if $d1.fuse() == 0 {
486-
assert_eq!($d1.rolls().len(), $d2.rolls().len());
487-
}
481+
assert!($d1.similar(&$d2));
488482
};
489483
}
490484

@@ -498,11 +492,6 @@ pub mod test {
498492
expect_dice_similar!("1d6", Some(Dice { count: 1, sides: 6, fuse: 0, rolls: vec![1], total: 1, keep: 0 }));
499493
}
500494

501-
#[test]
502-
fn r_1d6() {
503-
expect_dice_similar!("1d6", Dice::new_extended(1, 6, 0, 0));
504-
}
505-
506495
#[test]
507496
fn r_1d6_exploding() {
508497
expect_dice_similar!("1d6!", Dice::new_extended(1, 6, 0, 6));
@@ -518,6 +507,12 @@ pub mod test {
518507
expect_dice_similar!("1df", Dice::new_extended(1, 0, 0, 0));
519508
}
520509

510+
#[test]
511+
fn r_1df_count() {
512+
let f = "1df".parse::<Dice>().unwrap();
513+
assert!(f.sides == 0);
514+
}
515+
521516
#[test]
522517
fn r_1df_caps() {
523518
expect_dice_similar!("1DF", Dice::new_extended(1, 0, 0, 0));

src/rollset.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Display for DiceWords {
4141
DiceWords::Bonus(d) => write!(fmt, "{}", d),
4242
DiceWords::Multiplier(d) => write!(fmt, "{}", d),
4343
DiceWords::Plus => write!(fmt, "+"),
44-
DiceWords::Minus => write!(fmt, "-"),
44+
DiceWords::Minus => write!(fmt, "\u{2212}"),
4545
DiceWords::Times => write!(fmt, "\u{00d7}"),
4646
DiceWords::Other(s) | DiceWords::Comment(s) => write!(fmt, "|{}|", s),
4747
DiceWords::Total(t) => write!(fmt, "= {}", t),
@@ -73,7 +73,7 @@ impl FromStr for DiceWords {
7373
}
7474
}
7575

76-
#[derive(Clone, Debug, PartialEq)]
76+
#[derive(Clone, Debug, PartialEq, Eq)]
7777
pub enum RollParseError {
7878
DiceError(DiceParseError),
7979
InvalidOrder,
@@ -567,6 +567,34 @@ mod test {
567567
assert!(matches!(rs.words[3], DiceWords::Total(_)));
568568
}
569569

570+
#[test]
571+
fn regular_minus() {
572+
let rs = "d8-2".parse::<RollSet>().unwrap();
573+
assert_eq!(rs.words.len(), 4);
574+
575+
let roll = unwrap_dice!(&rs.words[0]);
576+
assert_eq!(roll.rolls().len(), 1);
577+
assert_eq!(roll.sides(), 8);
578+
579+
assert_eq!(rs.words[1], DiceWords::Minus);
580+
assert_eq!(rs.words[2], DiceWords::Bonus(2));
581+
assert!(matches!(rs.words[3], DiceWords::Total(_)));
582+
}
583+
584+
#[test]
585+
fn unicrud_minus() {
586+
let rs = "d8\u{2212}2".parse::<RollSet>().unwrap();
587+
assert_eq!(rs.words.len(), 4);
588+
589+
let roll = unwrap_dice!(&rs.words[0]);
590+
assert_eq!(roll.rolls().len(), 1);
591+
assert_eq!(roll.sides(), 8);
592+
593+
assert_eq!(rs.words[1], DiceWords::Minus);
594+
assert_eq!(rs.words[2], DiceWords::Bonus(2));
595+
assert!(matches!(rs.words[3], DiceWords::Total(_)));
596+
}
597+
570598
#[test]
571599
fn test_only_penalty() {
572600
let rs = "3d8-2".parse::<RollSet>().unwrap();
@@ -754,7 +782,7 @@ mod test {
754782
#[test]
755783
fn many_rollv_str() {
756784
let rs = "d1 + 2d1 - 4 to hit".parse::<RollSet>().unwrap();
757-
assert_eq!("1d1 [1] + 2d1 [2] - 4 |to hit| = -1", format!("{}", rs));
785+
assert_eq!("1d1 [1] + 2d1 [2] \u{2212} 4 |to hit| = -1", format!("{}", rs));
758786
}
759787

760788
#[test]
@@ -778,7 +806,7 @@ mod test {
778806

779807
#[test]
780808
fn more_like_chat() {
781-
let line = "/roll d1 + 2d1 - 4 to hit; 1d1 for damage";
809+
let line = "/roll d1 + 2d1 \u{2212} 4 to hit; 1d1 for damage";
782810
let chatter: Vec<&str> = line.splitn(2, char::is_whitespace).collect();
783811
assert_eq!(chatter.len(), 2);
784812
assert!(chatter[0].starts_with("/r"));
@@ -793,7 +821,7 @@ mod test {
793821
});
794822

795823
assert!(rolls.is_ok());
796-
assert_eq!(rolls.unwrap(), "rolls 1d1 [1] + 2d1 [2] - 4 |to hit| = -1; 1d1 [1] |for damage| = 1");
824+
assert_eq!(rolls.unwrap(), "rolls 1d1 [1] + 2d1 [2] \u{2212} 4 |to hit| = -1; 1d1 [1] |for damage| = 1");
797825
}
798826

799827
#[test]
@@ -822,7 +850,7 @@ mod test {
822850

823851
#[test]
824852
fn multiplier_other_ok() {
825-
assert!("1d20+4+1d6-3*1.5 for ham + 4 - 1d6 foo".parse::<RollSet>().is_ok());
853+
assert!("1d20+4+1d6-3*1.5 for ham + 4 \u{2212} 1d6 foo".parse::<RollSet>().is_ok());
826854
}
827855

828856
#[test]

0 commit comments

Comments
 (0)