summary refs log tree commit diff stats
path: root/test/tc_ext.py
blob: c57d4aa8a6af32397b6fd0dfa7a95ef2e372d495 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
if __name__ == '__main__': from __init__ import init; init()
import unittest
from collections import deque

from ranger.ext.iter_tools import *

class TestCases(unittest.TestCase):
	def test_flatten(self):
		def f(x):
			return list(flatten(x))

		self.assertEqual(
			[1,2,3,4,5],
			f([1,2,3,4,5]))
		self.assertEqual(
			[1,2,3,4,5],
			f([1,[2,3],4,5]))
		self.assertEqual(
			[1,2,3,4,5],
			f([[1,[2,3]],4,5]))
		self.assertEqual(
			[],
			f([[[[]]]]))
		self.assertEqual(
			['a', 'b', 'fskldfjl'],
			f(['a', ('b', 'fskldfjl')]))
		self.assertEqual(
			['a', 'b', 'fskldfjl'],
			f(['a', deque(['b', 'fskldfjl'])]))
		self.assertEqual(
			set([3.5, 4.3, 5.2, 6.0]),
			set(f([6.0, set((3.5, 4.3)), (5.2, )])))

	def test_unique(self):
		def u(x):
			return list(unique(x))

		self.assertEqual(
			[1,2,3],
			u([1,2,3]))
		self.assertEqual(
			[1,2,3],
			u([1,2,3,2,1]))
		self.assertEqual(
			[1,2,3],
			u([1,2,3,1,2,3,2,2,3,1,2,3,1,2,3,2,3,2,1]))
		self.assertEqual(
			[1,[2,3]],
			u([1,[2,3],1,[2,3],[2,3],1,[2,3],1,[2,3],[2,3],1]))

	def test_unique_keeps_type(self):
		def u(x):
			return unique(x)

		self.assertEqual(
			[1,2,3],
			u([1,2,3,1]))
		self.assertEqual(
			(1,2,3),
			u((1,2,3,1)))
		self.assertEqual(
			set((1,2,3)),
			u(set((1,2,3,1))))
		self.assertEqual(
			deque((1,2,3)),
			u(deque((1,2,3,1))))

	def test_mount_path(self):
		# assuming ismount() is used

		def my_ismount(path):
			depth = path.count('/')
			if path.startswith('/media'):
				return depth == 0 or depth == 2
			return depth <= 1

		from ranger.ext import mount_path
		original_ismount = mount_path.ismount
		mount_path.ismount = my_ismount
		try:
			mp = mount_path.mount_path

			self.assertEqual('/home', mp('/home/hut/porn/bondage'))
			self.assertEqual('/', mp('/'))
			self.assertEqual('/media/sdb1', mp('/media/sdb1/foo/bar'))
			self.assertEqual('/media/sdc2', mp('/media/sdc2/a/b/c/d/e'))
		finally:
			mount_path.ismount = original_ismount

		# TODO: links are not tested but I don't see how its possible
		# without messing around with mounts.
		# self.assertEqual('/media/foo',
		#     mount_path('/media/bar/some_link_to_a_foo_subdirectory'))

	def test_openstruct(self):
		from ranger.ext.openstruct import OpenStruct
		from random import randint, choice
		from string import ascii_letters

		os = OpenStruct(a='a')
		self.assertEqual(os.a, 'a')
		self.assertRaises(AttributeError, getattr, os, 'b')

		dictionary = {'foo': 'bar', 'zoo': 'zar'}
		os = OpenStruct(dictionary)
		self.assertEqual(os.foo, 'bar')
		self.assertEqual(os.zoo, 'zar')
		self.assertRaises(AttributeError, getattr, os, 'sdklfj')

		for i in range(100):
			attr_name = ''.join(choice(ascii_letters) \
				for x in range(randint(3,9)))
			value = randint(100,999)
			if not attr_name in os:
				self.assertRaises(AttributeError, getattr, os, attr_name)
			setattr(os, attr_name, value)
			value2 = randint(100,999)
			setattr(os, attr_name, value2)
			self.assertEqual(value2, getattr(os, attr_name))


if __name__ == '__main__':
	unittest.main()
an>.c.Message(*w.selected, key) if err != nil { return false, err } // setup parts of the message to use in the search // this is so that we try to minimise reading unnecessary parts var ( flags []maildir.Flag header *models.MessageInfo body string all string ) if parts&FLAGS > 0 { flags, err = message.Flags() if err != nil { return false, err } } if parts&HEADER > 0 { header, err = message.MessageInfo() if err != nil { return false, err } } if parts&BODY > 0 { // TODO: select which part to search, maybe look for text/plain reader, err := message.NewBodyPartReader([]int{1}) if err != nil { return false, err } bytes, err := ioutil.ReadAll(reader) if err != nil { return false, err } body = string(bytes) } if parts&ALL > 0 { reader, err := message.NewReader() if err != nil { return false, err } bytes, err := ioutil.ReadAll(reader) if err != nil { return false, err } all = string(bytes) } // now search through the criteria // implicit AND at the moment so fail fast if criteria.Header != nil { for k, v := range criteria.Header { headerValue := header.RFC822Headers.Get(k) for _, text := range v { if !containsSmartCase(headerValue, text) { return false, nil } } } } if criteria.Body != nil { for _, searchTerm := range criteria.Body { if !containsSmartCase(body, searchTerm) { return false, nil } } } if criteria.Text != nil { for _, searchTerm := range criteria.Text { if !containsSmartCase(all, searchTerm) { return false, nil } } } if criteria.WithFlags != nil { for _, searchFlag := range criteria.WithFlags { if !containsFlag(flags, searchFlag) { return false, nil } } } if criteria.WithoutFlags != nil { for _, searchFlag := range criteria.WithoutFlags { if containsFlag(flags, searchFlag) { return false, nil } } } return true, nil } // Returns true if searchFlag appears in flags func containsFlag(flags []maildir.Flag, searchFlag maildir.Flag) bool { match := false for _, flag := range flags { if searchFlag == flag { match = true } } return match } // Smarter version of strings.Contains for searching. // Is case-insensitive unless substr contains an upper case character func containsSmartCase(s string, substr string) bool { if hasUpper(substr) { return strings.Contains(s, substr) } return strings.Contains(strings.ToLower(s), strings.ToLower(substr)) } func hasUpper(s string) bool { for _, r := range s { if unicode.IsUpper(r) { return true } } return false } // The parts of a message, kind of type MsgParts int const NONE MsgParts = 0 const ( FLAGS MsgParts = 1 << iota HEADER BODY ALL ) // Returns a bitmask of the parts of the message required to be loaded for the // given criteria func getRequiredParts(criteria *searchCriteria) MsgParts { required := NONE if len(criteria.Header) > 0 { required |= HEADER } if criteria.Body != nil && len(criteria.Body) > 0 { required |= BODY } if criteria.Text != nil && len(criteria.Text) > 0 { required |= ALL } if criteria.WithFlags != nil && len(criteria.WithFlags) > 0 { required |= FLAGS } if criteria.WithoutFlags != nil && len(criteria.WithoutFlags) > 0 { required |= FLAGS } return required }