SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Student:
  2.     int grade
  3.  
  4.     boolean IsStudentPassing():
  5.         return grade >= MIN_PASSING_GRADE
  6.  
  7.  
  8. ...
  9.  
  10. // In another file
  11. constant int MIN_PASSING_GRADE = 70
  12.    
  13. ensure that IsStudentPassing is false when grade is MIN_PASSING_GRADE - 1
  14. ensure that IsStudentPassing is true when grade is MIN_PASSING_GRADE
  15.    
  16. ensure that IsStudentPassing is false when grade is 69
  17. ensure that IsStudentPassing is true when grade is 70
  18.    
  19. use MooseX::Declare;
  20. class Student {
  21.     has grade => (
  22.         is => 'ro', isa => 'Num', required => 1,
  23.     );
  24.  
  25.     method min_passing_grade {
  26.         return MIN_PASSING_GRADE;
  27.     )
  28.  
  29.     method is_student_passing () {
  30.         return $self->grade >= $self->min_passing_grade
  31.     }
  32. }
  33.  
  34. class t::Student {
  35.     use Test::Sweet;
  36.     use Test::MockObject::Extends;
  37.  
  38.     test correctly_determines_student_is_passing () {
  39.         my $student = $self->_make_student($self->_passing_grade);
  40.  
  41.         ok($student->is_student_passing);
  42.     }
  43.  
  44.     method _make_student (Num $grade) {
  45.         my $student = Test::MockObject::Extends->new(
  46.             $student->new(grade => $grade)
  47.         );
  48.         # Here's the important line:
  49.         $student->set_always(
  50.             min_passing_grade => $self->_passing_grade
  51.         );
  52.         return $student;
  53.     }
  54.  
  55.     method _passing_grade () { 40 }
  56.  
  57.     test correctly_determines_student_is_failing () {
  58.         my $student = $self->_make_student($self->_passing_grade - 1);
  59.  
  60.         ok(not $student->is_student_passing);
  61.     }
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top