Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I posted this question on stackoverflow and it got a poor reception. In reality, the problems I'm facing are minor and any workaround can be considered impractical. However, I believe it might be of general interest in the abstract computer science domain, so I'm giving it a go here.

I'm working with Drupal's automated testing in PHP. It's a class that has two methods I'm concerned about: pass() and fail(). Each one produces a separate line on a results page indicating whether a particular operation passed or failed, e.g.

if ( $value == "expected_value" ) {
  $this->pass("Name of Test");
} else {
  $this->fail("Name of Test");
}

(The reason drupal does it this way is so that in the results, you can see "Name of Test" in a green bar if it passed, and in a red bar if it failed. In other words "Testing Feature A: failed" or "Test of Feature A: passed". Of course you can put two different messages in there, but in my situation, I want to put the same message in)

What I notice about the above is that it seems verbose, in that I repeat the same message twice. It seems to me that there should be a way of expressing this where I only declare the message once. Of course I could put it in a variable, but that takes up extra space and I pass it twice, also.

I'd like to do something like ternary assignment, only instead where I decide which function to call. Is there a way I can define this logic while only expressing Name of Test one time?

Something like:

$function = ($value == "expected_value") ? "pass" : "fail" ;
$this->$function("Name of Test");

Is this the most concise, elegant way I can phrase this logic? I tried

$this->(($value == "expected_value") ? "pass" : "fail")("Name of Test");

But I got a parse error. Is there a way to get it down to one line, like ternary assignment?

Edit Here's the best formulation of my question to date: "What's a syntax I can use, in any language or pseudo-code , to express this without saying Name of test twice?"

I feel like I should be able to express this logic without saying Name of Test twice (anywhere -- including within another function that I call elsewhere in code). It seems that PHP does not allow me to express this in this way, so I'm looking for examples in other languages (and people have already posted C, javascript, and python examples) that allow you to concisely express this without repeating yourself.

share|improve this question
2  
Here's a tidbit: in C, you can do (value == expected_value ? pass : fail)("Looking for expected value."), assuming pass and fail aren't macros. In C, function pointers are first-class values, but functions can't close over arguments. –  Joey Adams Oct 18 '11 at 1:59
1  
Hey Joey, why don't you put that in an answer? That's exactly the kind of information I'm looking for! :) I was wondering if other languages would allow me to express what I thought I should be able to in php. –  user1936 Oct 18 '11 at 2:01
    
Side note: Like you said, in your situation, you need the same message whether it passes or fails, so the whole if-else block is unnecessary. –  Ankit Soni Oct 18 '11 at 9:29
add comment

5 Answers 5

up vote 3 down vote accepted

Since you said in a comment you were curious about other languages as well, this works in javascript.

(value == "expected_value" ? pass : fail) ("Name of Test");

share|improve this answer
2  
Oh dear god, I'd shoot whoever wrote that on the spot ;) –  Demian Brecht Oct 18 '11 at 5:18
1  
Shoot him twice. (Once for me) –  WuHoUnited Oct 19 '11 at 2:12
add comment

If your language support first class functions that it can be done as simple as below Pseudo code:

function handleTest (bool result, string testName,Function pass, Function fail)
{
    result ? pass(testName) : fail(testName);
}

//Usage

handleTest($value == "expected_value","Name of Test", $this->pass,$this->fail);
share|improve this answer
    
What I was trying to ask was a syntax to express this without saying testName twice (anywhere, even if it's hidden in a function). –  user1936 Oct 18 '11 at 14:07
add comment

It's been a while since I've looked at PHP, but this should give you what you're looking for:

class Tester
{
    public function pass($s) {
        echo "pass $s\n";
    }

    public function fail($s) {
        echo "fail $s\n";
    }

    public function test($e, $v) {
        call_user_func($e == $v ? array($this, "pass") : array($this, "fail"), "my test");
    }
}
$t = new Tester();
$t->test(1, 1);
$t->test(2, 1);

ideone test here

Having said that, generally it's nice to have unit tests (and their associated framework as readable as possible. What you already have is more readable than the code above. If you're worried about duplication of the string, why not just store it in a local variable and pass it to pass or fail?

share|improve this answer
    
As a practical issue it doesn't really matter; I was trying to figure out how I could express this from an abstract syntax perspective. Duplication of the string is the same issue as duplication of the variable. –  user1936 Oct 18 '11 at 14:06
add comment

Python

(self.pass if value == "expected_value" else self.fail)("Name of Test")

Some languages have functions as first-class objects as well as class methods.

[The answer is short because the question isn't really a question, it's a review of the limitations of PHP with a "it might be of general interest" remark that somehow makes a long statement about PHP into a question about other languages. Since the question is unclear, it's hard to provide more answer. This short answer does not seek to dismiss your question. Moderators have warned me that short answers are dismissive. Since I can't provide more answer, I can only apologize in advance and hope that everyone understands that I'm not being dismissive. The question is difficult to to understand and answer.]

share|improve this answer
    
Here's a better formulation of the question: "What's a syntax I can use to express this without saying Name of test twice?" –  user1936 Oct 18 '11 at 14:08
    
Actually, I was looking over my original question, and I don't think it's all that difficult to understand: "Is there a way I can define this logic while only expressing Name of Test one time?" And you got the answer right off the bat! –  user1936 Oct 18 '11 at 14:26
    
@user1936: "I don't think it's all that difficult to understand". Sorry. I disagree. I find it very difficult to understand. My answer amounts to luck. –  S.Lott Oct 18 '11 at 18:17
    
You gave basically the same answer that others posted in c and javascript. That all three of you read my post and gave me the same answer at random seems like incredible odds. –  user1936 Oct 18 '11 at 20:23
    
I apologize, I didn't mean to get snippy. Does "What's a syntax I can use to express this without saying Name of test twice?" not make much sense either? I'm not saying it's a brilliant question; I'm just trying (and failing) to clarify. –  user1936 Oct 18 '11 at 20:47
show 1 more comment

Why not make a function that handles both cases, with a boolean argument to choose between them? In the case of Simpletest, it appears you can just say this:

$this->assertTrue($value == "expected_value", "Name of Test");
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.