Re: Unit testing errors (testing the platform module)

2010-04-14 Thread Terry Reedy

On 4/14/2010 11:19 AM, J. Cliff Dyer wrote:

On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:

self.assertEqual(platform.__builtins__.__class__, dict,
"platform.__class__ supposed to be dict")
self.assertEqual(platform.__name__, 'platform' )


The preferred spelling for:

 platform.__builtins__.__class__

would be

 type(platform.__builtins__)


Agreed


It's shorter and easier to read, but essentially says the same thing.

You can also use it on integer literals, which you can't do with your
syntax:

 >>>  type(1)
 
 >>>  1.__class__
 ...
 SyntaxError: invalid syntax


Add the needed space and it works fine.

>>> 1 .__class__


A possible use of literal int attributes is for bound mehods:

>>> inc = 1 .__add__
>>> inc(3)
4
>>> inc(3.0)
NotImplemented

Whereas def inc(n): return n+1 is generic and would return 4.0.

Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-14 Thread J. Cliff Dyer
On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
> self.assertEqual(platform.__builtins__.__class__, dict,
> "platform.__class__ supposed to be dict")
> self.assertEqual(platform.__name__, 'platform' ) 

The preferred spelling for:

platform.__builtins__.__class__ 

would be

type(platform.__builtins__)

It's shorter and easier to read, but essentially says the same thing.

You can also use it on integer literals, which you can't do with your
syntax:

>>> type(1)

>>> 1.__class__
...
SyntaxError: invalid syntax

Admittedly, this is a trivial benefit.  If you're using a literal, you
already know what type you're dealing with.

Cheers,
Cliff

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-14 Thread john maclean
On 14 April 2010 09:09, Gabriel Genellina  wrote:
> En Tue, 13 Apr 2010 11:01:19 -0300, John Maclean 
> escribió:
>
>> Is there an error in my syntax? Why is my test failing? Line 16.
>>
>> ==
>> FAIL: platform.__builtins__.blah
>> --
>> Traceback (most recent call last):
>>  File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
>>    self.assertEquals(platform.__builtins__.__class__, "")
>> AssertionError:  != ""
>>
>> --
>
> To express the condition "SOMEOBJECT must be a dictionary", use:
>
>    isinstance(SOMEOBJECT, dict)
>
> SOMEOBJECT might actually be an instance of any derived class and still pass
> the test; that's usually the desired behavior.
>
> In the rare cases where only a very specific type is allowed, use this form
> instead:
>
>    type(SOMEOBJECT) is dict
>
>
> The test case above should read then:
>
>    self.assert_(isinstance(platform.__builtins__, dict),
>                 type(platform.__builtins__))
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

This is cool. Thanks for your replies.

self.assertEqual(platform.__builtins__.__class__, dict,
"platform.__class__ supposed to be dict")
self.assertEqual(platform.__name__, 'platform' )


-- 
John Maclean
07739 171 531
MSc (DIC)

Enterprise Linux Systems Engineer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-14 Thread Gabriel Genellina
En Tue, 13 Apr 2010 11:01:19 -0300, John Maclean   
escribió:



Is there an error in my syntax? Why is my test failing? Line 16.

==
FAIL: platform.__builtins__.blah
--
Traceback (most recent call last):
  File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
self.assertEquals(platform.__builtins__.__class__, "")
AssertionError:  != ""

--


To express the condition "SOMEOBJECT must be a dictionary", use:

isinstance(SOMEOBJECT, dict)

SOMEOBJECT might actually be an instance of any derived class and still  
pass the test; that's usually the desired behavior.


In the rare cases where only a very specific type is allowed, use this  
form instead:


type(SOMEOBJECT) is dict


The test case above should read then:

self.assert_(isinstance(platform.__builtins__, dict),
 type(platform.__builtins__))

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-13 Thread J. Cliff Dyer
The problem is that the class of platform.__builtins__ is a dict, not a
string containing the text "".  

Try replacing line 16 with this:

self.assertEqual(type(platform.__builtins__), dict)

Cheers,
Cliff


On Tue, 2010-04-13 at 15:01 +0100, John Maclean wrote:
> I normally use  languages unit testing framework to get a better
> understanding of how a language works. Right now I want to grok the
> platform module;
> 
> 
>  1 #!/usr/bin/env python
>   2 '''a pythonic factor'''
>   3 import unittest
>   4 import platform
>   5
>   6 class TestPyfactorTestCase(unittest.TestCase):
>   7 def setUp(self):
>   8 '''setting up stuff'''
>  13
>  14 def testplatformbuiltins(self): 15
> '''platform.__builtins__.blah '''
>  16 self.assertEquals(platform.__builtins__.__class__, " ict'>")
>  17
>  18
>  19 def tearDown(self):
>  20 print 'cleaning stuff up'
>  21
>  22 if __name__ == "__main__":
>  23 unittest.main()
> 
> 
> Is there an error in my syntax? Why is my test failing? Line 16.
> 
> 
> python stfu/testing/test_pyfactor.py
> Fcleaning stuff up
> 
> ==
> FAIL: platform.__builtins__.blah
> --
> Traceback (most recent call last):
>   File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
> self.assertEquals(platform.__builtins__.__class__, "")
> AssertionError:  != ""
> 
> --
> Ran 1 test in 0.000s
> 
> FAILED (failures=1)
> 
> -- 
> John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
> 171 531


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-13 Thread MRAB

John Maclean wrote:

I normally use  languages unit testing framework to get a better
understanding of how a language works. Right now I want to grok the
platform module;


 1 #!/usr/bin/env python
  2 '''a pythonic factor'''
  3 import unittest
  4 import platform
  5
  6 class TestPyfactorTestCase(unittest.TestCase):
  7 def setUp(self):
  8 '''setting up stuff'''
 13
 14 def testplatformbuiltins(self): 15
'''platform.__builtins__.blah '''
 16 self.assertEquals(platform.__builtins__.__class__, "")
 17
 18
 19 def tearDown(self):
 20 print 'cleaning stuff up'
 21
 22 if __name__ == "__main__":
 23 unittest.main()


Is there an error in my syntax? Why is my test failing? Line 16.


python stfu/testing/test_pyfactor.py
Fcleaning stuff up

==
FAIL: platform.__builtins__.blah
--
Traceback (most recent call last):
  File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
self.assertEquals(platform.__builtins__.__class__, "")
AssertionError:  != ""

--
Ran 1 test in 0.000s

FAILED (failures=1)


platform.__builtins__.__class__ returns a dict, which is not the same as
"", a string.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-13 Thread Martin P. Hellwig

On 04/13/10 15:01, John Maclean wrote:

I normally use  languages unit testing framework to get a better
understanding of how a language works. Right now I want to grok the
platform module;


  1 #!/usr/bin/env python
   2 '''a pythonic factor'''
   3 import unittest
   4 import platform
   5
   6 class TestPyfactorTestCase(unittest.TestCase):
   7 def setUp(self):
   8 '''setting up stuff'''
  13
  14 def testplatformbuiltins(self): 15
'''platform.__builtins__.blah '''
  16 self.assertEquals(platform.__builtins__.__class__, "")
  17
  18
  19 def tearDown(self):
  20 print 'cleaning stuff up'
  21
  22 if __name__ == "__main__":
  23 unittest.main()


Is there an error in my syntax? Why is my test failing? Line 16.


python stfu/testing/test_pyfactor.py
Fcleaning stuff up

==
FAIL: platform.__builtins__.blah
--
Traceback (most recent call last):
   File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
 self.assertEquals(platform.__builtins__.__class__, "")
AssertionError:  != ""

--
Ran 1 test in 0.000s

FAILED (failures=1)



What happens if you change this line:
self.assertEquals(platform.__builtins__.__class__, "")

To something like:
self.assertEquals(platform.__builtins__.__class__, type(dict()))

or
self.assertEquals(str(platform.__builtins__.__class__), "")

--
mph

--
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing errors (testing the platform module)

2010-04-13 Thread Benjamin Kaplan
On Tue, Apr 13, 2010 at 10:01 AM, John Maclean  wrote:

> I normally use  languages unit testing framework to get a better
> understanding of how a language works. Right now I want to grok the
> platform module;
>
>
>  1 #!/usr/bin/env python
>  2 '''a pythonic factor'''
>  3 import unittest
>  4 import platform
>  5
>  6 class TestPyfactorTestCase(unittest.TestCase):
>  7 def setUp(self):
>  8 '''setting up stuff'''
>  13
>  14 def testplatformbuiltins(self): 15
> '''platform.__builtins__.blah '''
>  16 self.assertEquals(platform.__builtins__.__class__, "ict'>")
>  17
>  18
>  19 def tearDown(self):
>  20 print 'cleaning stuff up'
>  21
>  22 if __name__ == "__main__":
>  23 unittest.main()
>
>
> Is there an error in my syntax? Why is my test failing? Line 16.
>
>
Because you are checking if the type object dict is equal to the str object
"". A type object will never compare equal to a str object,
even though the string representation of them is the same.

>>> type({}) == ""
False
>>> type({})

>>> str(type({})) == ""
True
>>> type({}) == dict
True
>>> platform.__builtins__.__class__ == dict
True




>
> python stfu/testing/test_pyfactor.py
> Fcleaning stuff up
>
> ==
> FAIL: platform.__builtins__.blah
> --
> Traceback (most recent call last):
>  File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
>self.assertEquals(platform.__builtins__.__class__, "")
> AssertionError:  != ""
>
> --
> Ran 1 test in 0.000s
>
> FAILED (failures=1)
>
> --
> John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
> 171 531
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Unit testing errors (testing the platform module)

2010-04-13 Thread John Maclean
I normally use  languages unit testing framework to get a better
understanding of how a language works. Right now I want to grok the
platform module;


 1 #!/usr/bin/env python
  2 '''a pythonic factor'''
  3 import unittest
  4 import platform
  5
  6 class TestPyfactorTestCase(unittest.TestCase):
  7 def setUp(self):
  8 '''setting up stuff'''
 13
 14 def testplatformbuiltins(self): 15
'''platform.__builtins__.blah '''
 16 self.assertEquals(platform.__builtins__.__class__, "")
 17
 18
 19 def tearDown(self):
 20 print 'cleaning stuff up'
 21
 22 if __name__ == "__main__":
 23 unittest.main()


Is there an error in my syntax? Why is my test failing? Line 16.


python stfu/testing/test_pyfactor.py
Fcleaning stuff up

==
FAIL: platform.__builtins__.blah
--
Traceback (most recent call last):
  File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
self.assertEquals(platform.__builtins__.__class__, "")
AssertionError:  != ""

--
Ran 1 test in 0.000s

FAILED (failures=1)

-- 
John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
171 531
-- 
http://mail.python.org/mailman/listinfo/python-list